2015-09-04 98 views
-1

我怎么知道CheckBox被点击或没有点击? 感谢所有我怎么知道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

'((复选框)findViewById(R.id.checkBox).setOnCheckedChangedListener(...)' –

+0

可能重复[Android:复选框侦听器](http://stackoverflow.com/questions/8386832/android-checkbox-listener) –

回答

0
//declaring the checked variable at the top 
checked=false; 
if(checkbox.isChecked()) 
{ 
checked=true; 
} 
else 
{ 
checked=false; 
} 
+1

boolean checked = checkbox.isChecked(); – JesusS

0

试试这个

CheckBox cb = (CheckBox)findViewById(R.id.checkBox); 
if (cb.isChecked()) { 
    // code to run when checked 
} else { 
    // code to run when unchecked 
} 
+0

这是真的答案。如何设置点击在java代码? –

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(); 
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 
    }   
} 
} 

让我知道,如果它的工作原理...