在
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); } }
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); } };
解决方法
是的,这是可能的.我下面写了一个例子,应该比较简单.
像往常一样,为所有按钮添加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的语句将不正确.