0

在Android中,这段代码是否可以验证复选框是否被单击以取消选中?

      public void onClick(View v) {

        if (((CheckBox) v).isChecked()) {
            Toast.makeText(IdentifyActivity.this, "clicked to check", Toast.LENGTH_LONG);
        }
        else{
            Toast.makeText(IdentifyActivity.this, "clicked to uncheck", Toast.LENGTH_LONG);
        }
4

3 回答 3

3

你可以使用OnCheckedChangeListener. 你必须实现回调

 onCheckedChanged(CompoundButton buttonView, boolean isChecked)

这就是你的任务通常完成的方式。您的代码也可以正常工作,并且可能在两种方法之间没有真正的区别

于 2013-05-17T12:20:47.463 回答
0
CheckBox cb = (CheckBox) findViewById(R.id.myCheckBox);

cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // It's checked
        } else {
            // It's not checked
        }
    }
});
于 2013-05-17T12:30:50.477 回答
0
    checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

        if(isChecked==true)
         {
           //your checkbox is checked
          }
        else
         {
            //your checkbox is not checked
          }

   }
}
于 2013-05-17T12:31:07.550 回答