2011-01-30 17 views
5

複数のチェックボックスがありますが、1つのチェックボックスのみを選択してください。私はif-else文を試しますが、それは使用できません。だから、私はそのようになる方法を知りたい。チェックボックスを1つだけ選択してください

ありがとうございました

+0

なぜあなたはラジオボタンを使用していませんの? – jball

答えて

10

なぜラジオボタンを使用しないのですか?それらは正確に意味されています。

ただし、チェックボックスを使用することを主張する場合は、オンのイベントを変更してチェックボックスをオンにすると、他のチェックボックスが無効になります。

+0

それは同じではありませんか?だから、私はまだラジオボタンを無効にする必要がありますか?どうやってするか?ありがとうございました – Yoo

+2

ラジオボタンは通常、ラジオグループ内で一緒に使用されます。ラジオグループ内に複数のラジオボタンがある場合、ラジオボタンをチェックすると、他のラジオボタンがすべて自動的にチェックされなくなります。このURLをご覧ください:http://java.dzone.com/articles/google-android-tutorial?page=0,4 – TheCottonSilk

0

私はこれでした:

- 私のレイアウトでは:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:alignmentMode="alignBounds" 
    android:columnCount="1" 
    android:background="#ff99f6ff" 
    > 

    <ImageView 
     android:id="@+id/escudo" 
     android:layout_width="250dp" 
     android:layout_height="250dp" 
     android:layout_gravity="center_horizontal|fill_vertical" 
     android:src="@drawable/dicon2" 
     android:contentDescription="foto logo" 
     android:layout_marginTop="30dp" 
     android:layout_alignParentTop="false" 
     android:layout_centerInParent="true" /> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button" 
     android:layout_gravity="top" 
     android:layout_below="@+id/radioGroup" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="48dp" /> 

    <RadioGroup 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/radioGroup" 
     android:orientation="horizontal" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="34dp" 
     android:gravity="center_horizontal" 
     android:checkedButton="1" 
     > 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/check_a" 
      android:id="@+id/checkBoxA" 
      android:layout_above="@+id/escudo" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:onClick="onCheckboxClicked" /> 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/check_b" 
      android:id="@+id/checkBoxB" 
      android:layout_alignTop="@+id/checkBoxA" 
      android:layout_toRightOf="@+id/checkBoxA" 
      android:layout_toEndOf="@+id/checkBoxA" 
      android:onClick="onCheckboxClicked"/> 

     <CheckBox 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/check_c" 
      android:id="@+id/checkBoxC" 
      android:layout_alignTop="@+id/checkBoxB" 
      android:layout_toRightOf="@+id/checkBoxB" 
      android:layout_toEndOf="@+id/checkBoxB" 
      android:onClick="onCheckboxClicked"/> 

    </RadioGroup> 
</RelativeLayout> 


import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.CheckBox; 

public class MainActivity extends ActionBarActivity { 

    private CheckBox checkBoxA, checkBoxB, checkBoxC; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     checkBoxA = (CheckBox) findViewById(R.id.checkBoxA); 
     checkBoxB = (CheckBox) findViewById(R.id.checkBoxB); 
     checkBoxC = (CheckBox) findViewById(R.id.checkBoxC); 
    } 

    public void onCheckboxClicked(View view) { 

     switch(view.getId()) { 

      case R.id.checkBoxA: 

        checkBoxB.setChecked(false); 
        checkBoxC.setChecked(false); 

       break; 

      case R.id.checkBoxB: 

        checkBoxC.setChecked(false); 
        checkBoxA.setChecked(false); 

       break; 

      case R.id.checkBoxC: 

        checkBoxA.setChecked(false); 
        checkBoxB.setChecked(false); 

       break; 
     } 
    } 
} 
関連する問題