Android:获取选中的复选框值

时间:2012-02-23 10:50:09

标签: android

点击按钮时,我需要检查复选框值。 Java代码:

dualcamera1 = (CheckBox)findViewById(R.id.Camera1_DualDisplay);
dualcamera2 = (CheckBox)findViewById(R.id.Camera2_DualDisplay);
dualcamera3 = (CheckBox)findViewById(R.id.Camera3_DualDisplay);
dualcamera4 = (CheckBox)findViewById(R.id.Camera4_DualDisplay);
dualdisplay = (Button)dialog.findViewById(R.id.DisplayDualVideo);

5 个答案:

答案 0 :(得分:53)

简单:

static int m=0;
dualDisplay.setOnClickListener(new OnCli....{
     onClick()
     {
          if(dualcamera1.isChecked())
              m++; // you can save this as checked somewhere
          if(dualcamera2.isChecked())
              m++; // you can save this as checked somewhere
          if(dualcamera3.isChecked())
              m++; // you can save this as checked somewhere
          if(dualcamera4.isChecked())
              m++; // you can save this as checked somewhere
     }
});
if(m>2 || m<2)
      // show error message
else
      // code here

如果选中复选框的详细信息,则可以保存复选框的详细信息,如果选中,则仅保存为for循环。

答案 1 :(得分:29)

您可以在复选框上调用isChecked()以获取其状态。如果你想计算已经存储的复选框的数量,那么你可以存储在ArrayList中并循环遍历它。

答案 2 :(得分:23)

如果未在onCreate()方法中定义CheckBox,则必须使用以下内容:

boolean isChecked = ((CheckBox) findViewById(R.id.checkBox1)).isChecked()

答案 3 :(得分:11)

获取Checkbox值的正确选项是使用以下方法:

  

isChecked()视图的当前检查状态

dualcamera1.isChecked() 

isEnable()如果启用此视图,则返回True,否则返回false。

答案 4 :(得分:7)

这可能会对您有所帮助:

//Returns the enabled status for this view. 
dualcamera1.isEnabled()  //return True if this view is enabled, false otherwise. 

//Indicates whether the view is currently in pressed state.
dualcamera1.isPressed() //return True if this view is enabled, false otherwise.