How to use Onclick event when using tag

前端 未结 1 1855
花落未央
花落未央 2021-01-21 02:43

I have two java class and two layout for both the class. Each layout is having one button in it. Both classes

相关标签:
1条回答
  • 2021-01-21 03:17

    First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:

    View includeView = (View)findViewById(R.id.clicked);
    Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
    Button button2 = (Button)includeView.findViewById(R.id.button2ID);
    

    And then set their onClickListeners

    button1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    //code whatever you want to do here
                }
            });
    
     button2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    //code whatever you want to do here
                }
            });
    

    ** EDIT **

    Fixed the typo. Should be includeView on the findViewById. Good explanation though!

    0 讨论(0)
提交回复
热议问题