2016-11-22 51 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

相關問題