在android中,我可以为不同的按钮使用相同的OnClickListener吗?如果是,我如何从哪个按钮生成点击?我目前有4个按钮,每个按钮都有自己的OnClickListener。除了单击按钮的文本之外,每个OnClickListener都执行相同的操作。我想创建一个OnClickListener,但我无法弄清楚如何确定单击哪个按钮。谢谢
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
});
setContentView(R.layout.home);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
//Load First Word
button1.setOnClickListener(button1ClickListener);
button2.setOnClickListener(button2ClickListener);
button3.setOnClickListener(button3ClickListener);
button4.setOnClickListener(button4ClickListener);
}
侦听器的代码,其中不同部分用粗体
private OnClickListener button1ClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
**Button button = (Button)findViewById(R.id.button1);**
handleButtonClick(button);
}
};
private OnClickListener button2ClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
**Button button = (Button)findViewById(R.id.button2);**
handleButtonClick(button);
}
};
private OnClickListener button3ClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
**Button button = (Button)findViewById(R.id.button3);**
handleButtonClick(button);
}
};
private OnClickListener button4ClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
**Button button = (Button)findViewById(R.id.button4);**
handleButtonClick(button);
}
};
handleButtonclick的代码
private void handleButtonClick(Button button) {
if(button.getText().equals(currentWord)){
currentScore += availableScore;
TextView score = (TextView)findViewById(R.id.textViewScore);
score.setText(String.valueOf(score));
currentIndex++;
availableScore = 4;
InitializeGame();
}
else{
availableScore--;
button.setEnabled(false);
}
}
根据Karthik的推荐,我将我的代码修改为以下内容:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
});
setContentView(R.layout.home);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
//Load First Word
button1.setOnClickListener(buttonClickListener);
button2.setOnClickListener(buttonClickListener);
button3.setOnClickListener(buttonClickListener);
button4.setOnClickListener(buttonClickListener);
}
OnClickListenerCode
private OnClickListener buttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
handleButtonClick((Button)arg0);
}
};
答案 0 :(得分:9)
是的,这是可能的。我在下面写了一个应该相对简单的例子。
像往常一样,为所有按钮添加OnClickListener,如下所示:
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
然后添加onClick()事件,如下所示:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == btn1){
//Things to do
}
if(v == btn2){
//Things to do
}
if(v == btn3){
//Things to do
}
}
这应该可以正常工作。不要忘记为你的onCreate所在的类实现View.OnClickListener,否则所有那些设置上面的OnClickListener的语句都是不正确的。
答案 1 :(得分:3)
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
});
setContentView(R.layout.home);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
//Load First Word
button1.setOnClickListener(button1ClickListener);
button2.setOnClickListener(button2ClickListener);
button3.setOnClickListener(button3ClickListener);
button4.setOnClickListener(button4ClickListener);
}
@Override
public void onClick(View v)
{
int id = v.getId();
switch (id) {
case R.id.button1:
//your code
break;
case R.id.button2:
break;
case R.id.button3:
break;
}
}