2018/11/18

Kotlin:
- Add a button's action in Fragment (config in XML does not work)
- start a new activity from a Fragment


class MainFragment : Fragment() {
//...
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val view: View = inflater.inflate(R.layout.main_fragment, container, false)
// add button click action by code
// setup action in XML does not work
val btn : Button = view.findViewById(R.id.button1)
btn.setOnClickListener { view: View ->
// start up a new activity
val intent = Intent(requireContext(), Gu1Activity::class.java)
requireContext().startActivity(intent)
}
return view
}
// ...
}
view raw MainFragment.kt hosted with ❤ by GitHub

No comments:

Post a Comment

Post Code on Blogger

Simplest way to post code to blogger for me: <pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black;overflow-x:...