I want to know if there is a method which acts similar to the onOptionsItemSelected method, but instead listening for the Options Menu items, it listens to all the buttons implemented in the UI.
onOptionsItemSelected() method :
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case R.id.item1:
if( currentPlayer == 1 ) {return true;}
updateUI( 1 );
return true;
case R.id.item2:
if( currentPlayer == 2 ) {return true;}
updateUI( 2 );
return true;
case R.id.item3:
if( currentPlayer == 3 ) {return true;}
updateUI( 3 );
return true;
case R.id.item4:
if( currentPlayer == 4 ) {return true;}
updateUI( 4 );
return true;
default:
return super.onContextItemSelected( item );
}
}
Right now, I’m stuck using this method for every single button, and I have way too many. It will be much cleaner if I can find a method that works like the one above.
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
1 Answer