How to open layout on button click (android)

前端 未结 3 1452
自闭症患者
自闭症患者 2020-12-28 16:48

How can I open another layout xml file when I click on a button in main.xml file?

so if I have main.xml which has a button sying click here and I click it, it opens

3条回答
  •  攒了一身酷
    2020-12-28 17:18

    A Kotlin way:

    -Add the onClick event directly in the designer.

    1. Open the activity (Activity1.xml for example) file in the designer mode
    2. Select the button that will trigger the transition
    3. Add the function name on the onClick box of the right panel with all the button properties

    -Open the Activity .kt file

    1. Add the function with the name that you just defined in the designer

      fun openActivity2(view: View) { intent = Intent(view.context,Activity2::class.java) startActivity(intent) }

    Now you have the function linked to the onClick event of your button

提交回复
热议问题