2016-11-22 50 views
0

我在对话框中有两个复选框。我想为这些复选框设置单个检查。这意味着如果我选中了复选框1,复选框2将被取消选中。当我检查复选框2时类似。如果选中复选框1,则check_value变量设置为true,并且在复选框2选中时为false。如何在Android对话框中选中复选框?

这是我的代码,但我的预期

@Override 
public Dialog onCreateDialog(final Bundle savedInstanceState) { 
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    final View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialogView, null); 
    boolean check_value=false; 

    checkbox1 = (CheckBox) dialogView.findViewById(R.id.checkbox1); 
    checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      checkbox2.setChecked(false); 
      checkbox1.setChecked(true); 
      check_value=true; 
     } 
    }); 
    checkbox2 = (CheckBox) dialogView.findViewById(R.id.checkbox2); 
    checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      checkbox2.setChecked(true); 
      checkbox1.setChecked(false); 
      check_value=false; 
     } 
    }); 
} 

这是我的xml文件,它无法实现。默认情况下,复选框1为true

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_below="@+id/list" 
     android:layout_centerInParent="true"> 
    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/checkbox1" 
     android:checked="true" 
     android:layout_marginLeft="2dp"/> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/checkbox2" 
     android:checked="false" 
     android:layout_marginLeft="2dp" /> 
</LinearLayout> 
+0

为什么复选框而不是单选按钮?他们是为此目的而生的 –

+0

你能用单选按钮来回答我的问题吗?请注意,我将它们放在对话框中 – user3051460

+0

对不起,现在我没有编译器。只需看看[这篇文章如何在构建器中使用自定义布局](http://stackoverflow.com/questions/22655599/alertdialog-builder-with-custom-layout-and-edittext-cannot-access-view)你也可以参考[这个链接也](http://stackoverflow.com/questions/27785167/radio-buttons-in-android-studio)如何使用单选按钮。希望这有助于 –

回答

1

试试这个:

@Override 
public Dialog onCreateDialog(final Bundle savedInstanceState) { 
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
final View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialogView, null); 
boolean check_value=false; 

checkbox1 = (CheckBox) dialogView.findViewById(R.id.checkbox1); 
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if(isChecked){ 
      checkbox2.setChecked(false); 
      check_value=true; 
     } 
     else 
      checkbox2.setChecked(true); 
    } 
}); 
checkbox2 = (CheckBox) dialogView.findViewById(R.id.checkbox2); 
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     if(isChecked){ 
      checkbox1.setChecked(false); 
      check_value=false; 
     } 
     else 
      checkbox1.setChecked(true); 

    } 
});} 
+0

这是正确的。谢谢 – user3051460

+0

等一下。你瘦身check_value是否适合设置? – user3051460

+0

你用'check_value'做了什么? –

0

您应该使用带有2个单选按钮的无线电组。这有你正在寻找的默认功能。复选框并不意味着这一点。

0

在Android中,您可以使用“android.widget.CheckBox”类来呈现复选框。

在本教程中,我们向您展示如何在XML文件中创建3个复选框,并演示如何使用侦听器来检查复选框状态 - 选中还是取消选中。

https://www.mkyong.com/android/android-checkbox-example/

0

不喜欢下面的代码...

<RadioGroup 
     android:id="@+id/myRadioGroup" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5sp" 
     android:orientation="horizontal"> 
     <RadioButton 
      android:id="@+id/rbTrue" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Accept"/> 
     <RadioButton 
      android:id="@+id/rbFalse" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:text="Reject"/> 
    </RadioGroup> 

然后在活动确实喜欢......

这里radioGroup中的对象由单选按钮组成的RadioGroup

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if(checkedId==R.id.rbTrue){ 
       check_value = true; 
      }else{ 
       check_value = false; 
      } 
     } 
    }); 
+0

'R.id.rbTrue'在DialogView中不起作用。我之前使用过它 – user3051460

相关问题