How to determine which button pressed on android

前端 未结 5 709
闹比i
闹比i 2020-12-01 09:38

i need to know, how to recognize, which button is pressed. Like if i have two buttons ,say button 1 and button2,and both of them performing the same method, say method(),how

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 10:00

    OR... you can just put a android:onClick="foo" in the xml code of the button, and define a method on java with the signature. Inside the method foo, get the id and compare it with the one you need

    public void foo(View v){
    
    if (v.getId() == R.id.yourButton){
    
    }
    
    else if (v.getId() == R.id.nextButton){
    
    }
    
    }
    

提交回复
热议问题