2012-06-14 32 views
11

Tôi đang cố gắng xây dựng RadioGroup trong Android với một RadioButton được kiểm tra theo mặc định. Tôi tự hỏi liệu điều này có thể thực hiện thông qua XML chứ không phải là lập trình hay không.RadioGroup checkedButton property

Đoạn mã sau đây dường như không làm việc như tôi nhận được một lỗi:

error: Error: No resource found that matches the given name (at 'checkedButton' with value '@id/rdb_positive') 

Mã này là:

<RadioGroup 
    style="@style/FormInputField" 
    android:orientation="vertical" 
    android:checkedButton="@id/rdb_positive"> <!-- Error on this line --> 
    <RadioButton 
     android:id="@+id/rdb_positive" 
     android:text="@string/answer_positive" /> 
    <RadioButton 
     android:id="@+id/rdb_negative" 
     android:text="@string/answer_negative" /> 
</RadioGroup> 

Nó không có ý nghĩa trong một cách, như id của RadioButton được định nghĩa sau khi thuộc tính trong RadioGroup được thiết lập, nhưng sau đó tôi tự hỏi tại sao có thuộc tính như vậy có sẵn.

Trả lời

23

Sử dụng android:checkedButton="@+id/rdb_positive", tôi nghĩ rằng bạn thêm + dấu hiệu sau đó tác phẩm của mình

+7

Tôi nghĩ + phào nhẹ được sử dụng để xác định một id mới, tôi ngạc nhiên nó hoạt động :-) Cảm ơn. – Ruben

+3

@LalitPoptani tôi biết rằng '+' được sử dụng cho id mới và cũng bug.but giải pháp của nó được gán '+' –

+3

Khi bạn đã sử dụng '+' trong 'RadioGroup', id' rdb_positive' sẽ được tạo. Vì vậy, bạn không cần phải sử dụng '+' một lần nữa trong 'RadioButton'. Vì vậy, hãy sử dụng 'android: id =" @ id/rdb_positive "' trong RadioButton. – faizal

2

thử này ......

<RadioGroup 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <RadioButton 
     android:id="@+id/rdb_positive" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:text="answer_positive" /> 

    <RadioButton 
     android:id="@+id/rdb_negative" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="answer_negative" /> 
</RadioGroup> 
+2

Vui lòng lưu ý: Nếu bạn đặt android: checked = "true" trên RadioButton mặc định, nó sẽ không được bỏ chọn khi kiểm tra khác! Để thực hiện nút mặc định đúng cách, bạn phải sử dụng thuộc tính "android: checkedButton" của RadioGroup. – cody

2

Bạn có thể thoát khỏi lỗi đó bằng cách tuyên bố id rdb_positive bên ids.xml và sau đó tham chiếu id từ cả hai yếu tố RadioGroupRadioButton.

<RadioGroup 
    style="@style/FormInputField" 
    android:orientation="vertical" 
    android:checkedButton="@id/rdb_positive"> <!-- REFERENCE TO ids.xml --> 
    <RadioButton 
     android:id="@id/rdb_positive" 
     android:text="@string/answer_positive" /> <!-- REFERENCE TO ids.xml --> 
    <RadioButton 
     android:id="@+id/rdb_negative" 
     android:text="@string/answer_negative" /> 
</RadioGroup> 

ids.xml:

<resources> 
    <item type="id" name="rdb_positive" /> 
</resources> 
+0

chỉ cần thêm dấu "+" vào id. android: checkedButton = "@ + id/rdb_positive" – agrosner

0
<RadioGroup 
    style="@style/FormInputField" 
    android:orientation="vertical"> 
    <RadioButton 
     android:id="@+id/rdb_positive" 
     android:text="@string/answer_positive" 
     android:checked="true"/> 
    <RadioButton 
     android:id="@+id/rdb_negative" 
     android:text="@string/answer_negative" /> 
</RadioGroup> 

Thêm android: kiểm tra = "true" vào radiobutton mà bạn muốn thực hiện như mặc định

+0

Vui lòng lưu ý: Nếu bạn đặt android: checked = "true" trên RadioButton mặc định, nó sẽ không được bỏ chọn khi kiểm tra khác! Để thực hiện nút mặc định đúng cách, bạn phải sử dụng thuộc tính "android: checkedButton" của RadioGroup –

Các vấn đề liên quan