我如何知道点击或未点击CheckBox? 谢谢所有
xml文件:
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
java文件:
public class Setting extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
}
}
答案 0 :(得分:0)
//declaring the checked variable at the top
checked=false;
if(checkbox.isChecked())
{
checked=true;
}
else
{
checked=false;
}
答案 1 :(得分:0)
试试这个
CheckBox cb = (CheckBox)findViewById(R.id.checkBox);
if (cb.isChecked()) {
// code to run when checked
} else {
// code to run when unchecked
}
答案 2 :(得分:0)
try this hope it will help you:
checkbox mchk1=(CheckBox)findViewById(R.id.chk11);
Boolean cBox1;
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
cBox1=pref.getBoolean("check1", false);
if(cBox1==true){
mchk1.setChecked(true);
//Rb1.setChecked(true);
}
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor editor = pref.edit();
if(mchk1.isChecked() ){
editor.putBoolean("check1", true);
}
else if(!mchk1.isChecked() )
{
editor.putBoolean("check1", false);
}
editor.commit();
答案 3 :(得分:0)
对您的活动进行如下更改,
public class Setting extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {
CheckBox myCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
myCheckBox = findViewById(R.id.checkBox);
myCheckBox.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//here the checkBox is checked
}else {
//here the checkBox is not checked
}
}
}
让我知道它是否有效......