Answer the question
In order to leave comments, you need to log in
Assigning a single clickListener to multiple buttons and a TextView. Why isn't it alive?
public class MainActivity extends Activity {
TextView textView1;
TextView textView2;
TextView textView3;
TextView textView4;
Button button1;
Button button2;
Button button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener myClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.button1:
textView1.setText(R.string.textOne);
break;
case R.id.button2:
textView2.setText(R.string.textTwo);
break;
case R.id.button3:
textView3.setText(R.string.textThre);
textView1.setText(R.string.textOneClick3);
textView4.setText(R.string.textClickable);
break;
case R.id.textView4:
textView4.setText(R.string.textClickable2);
break;
}
}
};
button1.setOnClickListener(myClickListener);
button2.setOnClickListener(myClickListener);
button3.setOnClickListener(myClickListener);
textView4.setOnClickListener(myClickListener);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question