How do I add a second button android?

前端 未结 2 617
傲寒
傲寒 2021-01-25 05:07

I´ve got a click listener to an email intent in my app, but I want to add a second button to do another thing? Sorry for the dum question, Im a begginer.

For the peoplew

2条回答
  •  执笔经年
    2021-01-25 05:23

    First define your button variables in your activity/fragment

    Button button1;
    Button button2;
    

    In the onCreate (activity) or onCreateView (fragment) method, link the button objects to the buttons in your layout, and set their onClickListeners:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        button1= (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                //do something
            }
        });
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                //do something else
            }
        });
    }
    

    Your layout file should then have something like this:

提交回复
热议问题