- Add a button's action in Fragment (config in XML does not work)
- start a new activity from a Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
// ... | |
} |
No comments:
Post a Comment