Reputation: 61
I am trying to learn Android Studio, and am making a basic OnClickListener. My project has an xml file containing various buttons, edit texts and check boxes. I am trying to make a single OnClickListener to be used by the three checkboxes. However, when I run the code below the only Toast message that appears says 'other', regardless of which box is pressed.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox maleBox, femaleBox, otherBox;
maleBox = findViewById(R.id.maleBox);
femaleBox = findViewById(R.id.femaleBox);
otherBox = findViewById(R.id.otherBox);
maleBox.setOnClickListener(this);
femaleBox.setOnClickListener(this);
otherBox.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.maleBox:
Toast.makeText(this, "man", Toast.LENGTH_SHORT).show();
case R.id.femaleBox:
Toast.makeText(this, "female", Toast.LENGTH_SHORT).show();
case R.id.otherBox:
Toast.makeText(this, "other", Toast.LENGTH_SHORT).show();
}
}
}
Does anyone have any ideas as to why this is occuring? Many thanks,
Upvotes: 0
Views: 63
Reputation: 34017
your code :
CheckBox maleBox, femaleBox, otherBox;
CheckBox dont suggest to use setOnClickListener
, should use setOnCheckedChangeListener()
void onCreate(){
//..
maleBox.setOnCheckedChangeListener(this);
femaleBox.setOnCheckedChangeListener(this);
otherBox.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.maleBox:
Toast.makeText(this, "man", Toast.LENGTH_SHORT).show();
break;
case R.id.femaleBox:
Toast.makeText(this, "female", Toast.LENGTH_SHORT).show();
break;
case R.id.otherBox:
Toast.makeText(this, "other", Toast.LENGTH_SHORT).show();
break;
}
}
Upvotes: 1
Reputation: 4620
When one of the case is matched in a switch statement, then it is the responsibility of the programmer to decide whether following cases after that 'case' must be executed or not, that flow of following cases can be stopped by using break; statement try below switch code,
switch (v.getId()){
case R.id.maleBox:
Toast.makeText(this, "man", Toast.LENGTH_SHORT).show();
break;
case R.id.femaleBox:
Toast.makeText(this, "female", Toast.LENGTH_SHORT).show();
break;
case R.id.otherBox:
Toast.makeText(this, "other", Toast.LENGTH_SHORT).show();
break;
}
In your scenario, the last toast is covering up the other toasts as it is the last case which will get executed every time, due to above explained reason
Upvotes: 1
Reputation: 4321
You are missing break
from your code.
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.maleBox:
Toast.makeText(this, "man", Toast.LENGTH_SHORT).show();
break;
case R.id.femaleBox:
Toast.makeText(this, "female", Toast.LENGTH_SHORT).show();
break;
case R.id.otherBox:
Toast.makeText(this, "other", Toast.LENGTH_SHORT).show();
break;
}
}
Read about switch here
Upvotes: 1