I'm currently trying to make a basic app that incorporates 4 toggle buttons and a textview. The idea is simple: When a button is pressed, the textview will show which buttons are currently being pressed. This can include multiple buttons (such as if, for example 1, 3, and 4 were pressed). Here's the code I've written for it:
TextView buttons;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons = (TextView)findViewById(R.id.textView1);
}
public void buttonPressed(View v){
boolean check = ((ToggleButton)v).isChecked();
int b1 = 1, b2 = 2, b3 = 3, b4 = 4;
switch(v.getId()){
case R.id.toggleButton1:
if(check){
buttons.setText("Buttons pressed: " + b1);
}
else{
buttons.setText("Buttons pressed:");
}
break;
case R.id.toggleButton2:
if(check){
buttons.setText("Buttons pressed: " + b2);
}
else{
buttons.setText("Buttons pressed:");
}
break;
case R.id.toggleButton3:
if(check){
buttons.setText("Buttons pressed: " + b3);
}
else{
buttons.setText("Buttons pressed:");
}
break;
case R.id.toggleButton4:
if(check){
buttons.setText("Buttons pressed: " + b4);
}
else{
buttons.setText("Buttons pressed:");
}
break;
default:
break;
}
}
Unfortunately, every time I run this program, the textview is overwritten, and I can't get it to validate that multiple buttons are pressed. All suggestions are welcome.