你好我是android的初学者。我在我的项目中使用了四个复选框。我希望在复选框3和4未选中状态后调用一个方法。如何编写该代码请帮助我。
提前致谢。
class myCheckBoxChnageClicker implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Is the view now checked?
boolean checked = ((CheckBox) buttonView).isChecked();
// Check which checkbox was clicked
switch(buttonView.getId()) {
case R.id.t3_check:
if (checked) {
} else {
}
break;
case R.id.t4_check:
if (checked) {
} else {
}
break;
case R.id.tsh_check:
if (checked) {
} else {
}
break;
case R.id.weight_check:
if(checked) {
} else {
}
break;
default:
}
}
public void showTextNotification(String msgToDisplay) {
if ((msgToDisplay.equals("Tsh_Check") && msgToDisplay.equals("Weight_Check"))) {
Toast.makeText(getApplicationContext(), "match", Toast.LENGTH_SHORT).show();
} else {
}
}
}
您好这是我的代码,我希望在3和 4复选框未选中状态时调用showTextNotification()
方法。
答案 0 :(得分:1)
您可以在复选框上调用isChecked()来获取其状态。
答案 1 :(得分:1)
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (checkBox2.isChecked() && isChecked) {
// Perform task
}
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (checkBox1.isChecked() && isChecked) {
// Perform task
}
}
});
答案 2 :(得分:1)
试试这个。
final boolean Check_three = false;
final boolean Check_foure = false;
class myCheckBoxChnageClicker implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Is the view now checked?
boolean checked = ((CheckBox) buttonView).isChecked();
// Check which checkbox was clicked
switch(buttonView.getId()) {
case R.id.t3_check:
if (checked) {
Check_three = true;
if (Check_three == true && Check_foure == true)
{
showTextNotification("printmeaage");
}
} else {
Check_three = false;
}
break;
case R.id.t4_check:
if (checked) {
Check_foure = true;
if (Check_three == true && Check_foure == true)
{
showTextNotification("printmeaage");
}
} else {
Check_foure = false;
}
break;
case R.id.tsh_check:
if (checked) {
} else {
}
break;
case R.id.weight_check:
if(checked) {
} else {
}
break;
default:
}
}