How to go about multiple Buttons and OnClickListeners
I have in 16 Button
s (numbers, plus, minus etc) in my layout XML file.
I'm wondering how to check which button was pressed.
My idea 开发者_如何学编程is, that I will for each button use onClick()
method but this method is a bit impractically, because I will have 16 of these onClick()
methods one for each Button.
Is there a more elegant way?
You can deal with them all in a single class that implements OnClickListener, or within the activity class if you like...
import android.view.View.OnClickListener;
public class MyActivity extends Activity implements OnClickListener {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonA = (Button) findViewById(R.id.buttonA);
buttonA.setOnClickListener(this);
Button buttonB = (Button) findViewById(R.id.buttonB);
buttonB.setOnClickListener(this);
}
//etc... etc...
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonA:
// do something
break;
case R.id.buttonB:
// do something else
break;
}
}
}
You can use one handler that you do not define as anonymous inner class, but in a separate class. onClick()
will get the view passed in and you can select from it.
public class MyActivity implements OnClickListener {
public void onClick(View v) {
Button b = (Button)v;
// do what you want
}
...
}
and then in your layout.xml just put for each button
<Button android:id=".."
android:onClick="onClick"
Sure. Create an array of your button-ids
and assign them the same listener (an implementation of View.OnClickListener
- defined as separate class, not as anonymous class) in a loop. In the listener you can check which is the pressed button (by comparing view
parameter in onClick()
method).
In activity:
MyOnClickListener myListener = new MyOnClickListener();
for (int id : buttonIdArray)
((Button)findViewById(id)).setOnClickListener(myListener);
In the onClick method:
int id = view.getId();
switch (id)
{
case ...:
// Do stuff
case ...:
// Do different stuff
}
You don't have to mention 16 on click listeners. Create a one and attach it to each button. onClick() method gets a View as a parameter. This view is your button. Just check for it's id or name.
import android.view.View.OnClickListener;
public class MyActivity extends Activity implements OnClickListener {
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonA = (Button) findViewById(R.id.buttonA);
Button buttonB = (Button) findViewById(R.id.buttonB);
buttonA.setOnClickListener(this);
buttonB.setOnClickListener(this);
}
public void onClick(View v) {
if(v==buttonA)
{
do something...
}
if(v==buttonB)
{
do something...
}
}
I usually do something like this:
@Override
public boolean onTouch(View v, MotionEvent event)
{
Button thisButton = (Button) v;
switch (event.ACTION_DOWN)
{
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < _button.length;i++)
if (tagOf(thisButton) == tagOf(_button[i]))
{
System.out.println("Button " + i + " pressed!");
_textView[i].setText(String.valueOf(_settingBrowser.countUpPosition(i)));
}
}
return false;
}
int tagOf (Button b)
{
return Integer.parseInt(b.getTag().toString());
}
Initiating the buttons with an ID number at the main class, like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_button = new Button[FIELDS];
_button[0] = (Button) findViewById(R.id.Button01);
_button[1] = (Button) findViewById(R.id.Button02);
_button[2] = (Button) findViewById(R.id.Button03);
_button[3] = (Button) findViewById(R.id.Button04);
_button[4] = (Button) findViewById(R.id.Button05);
_button[5] = (Button) findViewById(R.id.Button06);
_padListener = new SettingButtonListener(_button);
for (int j = 0; j < _button.length; j++){
_button[j].setTag(j);
_button[j].setOnTouchListener(_padListener);
}
}
This is neat if you have a big number of buttons like you do and want to use the same listener for all of them. Cheers!
精彩评论