Retrieving The Id Of The View That Was Clicked
I'm using MyActivity extends Activity implements OnClickListener{ This activity references around 10+ buttons and has setOnClicklistener(this) method called on every button. @Overr
Solution 1:
@Override
public void onClick(View v){
switch(v.getId()){
case R.id.btnCancel:
//your code for onclick of that button
break;
}
Solution 2:
you can use following method to get id.
v.getId()
Solution 3:
@Override
public void onClick(View v){
int id = v.getId();
if(id == R.id.button_ok){
}
}
Solution 4:
The View
parameter that is sent to your onClick
method is the actual button that was clicked, therefore you can check which one it is, for example:
@Override
public void onClick(View v){
switch(v.getId()) {
case R.id.button_1: ...; break;
case R.id.button_2: ...; break;
case R.id.button_3: ...; break;
...
default: //unknown button clicked
}
}
This is only one option, there are other. Search google for more info.
Solution 5:
use :
if(v.getId()==R.id.whatever)
{
// do something
}
or you can even use : Button btn = (Button)findViewById(R.id.btn);
if(v==btn)
{
// do something
}
but the second one is not recommended.
Post a Comment for "Retrieving The Id Of The View That Was Clicked"