2011-01-30 25 views
5

Tôi có nhiều hộp kiểm, nhưng tôi muốn người dùng chỉ chọn một hộp kiểm. Tôi thử câu lệnh if-else nhưng nó không thể sử dụng được. Vì vậy, tôi muốn biết, làm thế nào để được như thế.Chỉ chọn một hộp kiểm

Cảm ơn

+0

Tại sao bạn không sử dụng các nút radio? – jball

Trả lời

10

Tại sao bạn không sử dụng Nút radio thay thế? Họ có nghĩa là chính xác cho điều đó.

Nhưng nếu bạn nhấn mạnh vào việc sử dụng hộp kiểm, hãy sửa đổi sự kiện chọn hộp kiểm bất kỳ khi nào nó được chọn sẽ vô hiệu hóa các hộp kiểm khác.

+0

Nó không giống nhau? Vì vậy, tôi vẫn cần phải vô hiệu hóa nút radio phải không? Làm thế nào để làm điều đó? thanks ka – Yoo

+2

Các nút radio thường được sử dụng cùng nhau trong RadioGroup. Khi một số nút radio trực tiếp bên trong một nhóm radio, việc kiểm tra một nút radio sẽ tự động bỏ chọn tất cả các nút khác. Hãy xem URL này: http://java.dzone.com/articles/google-android-tutorial?page=0,4 – TheCottonSilk

0

Tôi đã làm điều này:

- Trong cách bố trí của tôi:

<?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; 
     } 
    } 
} 
Các vấn đề liên quan