0%

kotlin click事件 intent跳转 fragment获取控件

搬运自CSDN: kotlin click事件 intent跳转 fragment获取控件

click事件 intent跳转 传递参数

1
2
3
4
mFloatBtn.onClick {
val intent = Intent(this@MainContentActivity,MainActivity::class.java)
startActivity(intent)
}

只跳转,无参数传递

1
mFloatBtn.onClick { startActivity<MainActivity>() }

跳转,传参

1
2
3
4
5
mFloatBtn.onClick {
startActivity<MainActivity>(
"name" to "MainContent" // key to value
)
}

fragment获取控件

kotlin 在activity中,支持无需findviewbyid,控件直接可使用
但 fragment中,不支持,只好 find控件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private var mTxt: TextView? = null

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

val view = inflater!!.inflate(R.layout.frag_textview_content, null)
initView(view)
setUpViews()
return view
}

private fun initView(view: View) {
mTxt = view.find(R.id.mTxt)
}