How to open layout on button click (android)

前端 未结 3 1438
自闭症患者
自闭症患者 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:12

    -Inflate the button from the xml
    -add an onClickListener on it
    -set a new layout in the onClick event

    Button btn = (Button) findViewById(R.id.myButton);
    btn.setOnClickListener(new OnClickListener(){
    
    @Override
    public void onClick(View v)
    {
        MyActivity.setContentView(R.layout.newlayout);
    }
    
    });
    

    Something like this should work...

提交回复
热议问题