2011-10-31 152 views
0

我有一個4個靜態單選按鈕。 我想將自定義屬性添加到單選按鈕。Android單選按鈕自定義屬性

RadioButton btn_radio1= (RadioButton)findViewById(R.id.btn_radio1); 

//I just can change id of element,than get it. 
btn_radio1.setId(44); 

謝謝。

---修訂 XML

<RadioButton android:id="@+id/btn_radio1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TEST" /> 
+0

您錯過了右括號 – hovanessyan

+0

您最初是以XML佈局還是編程方式創建rButton? – hovanessyan

+0

我從xml使用它。從語法上我用於複選框,我很容易從數據庫 – Oyeme

回答

3

你不應該使用ID作爲此ID是由系統設置,不應該被改變爲存儲在Android組件/視圖中的數據。如果你想存儲你自己的ID,你可以使用setTag(...)的組件標籤屬性,或者從框架擴展RadioButton類來添加你自己的屬性。

+0

是的,我知道我不應該存儲數據在id.Now我試圖使用「setTag」感謝您的建議。 – Oyeme

1

就我的問題而言,除setId()之外,您無法訪問其他setter。 如果是這樣,你可能做錯了什麼。應該可以獲得單選按鈕(就像在你的代碼中一樣)並且可以訪問其他set方法,而不僅僅是setId(); 此鏈接可能會有所幫助:http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons

如果你想添加自定義屬性(例如做一個自定義組件,其行爲類似單選按鈕),看看這兩個鏈接如下:

1)http://hugman.posterous.com/parameterizing-a-custom-components-to-promote

2 )http://developer.android.com/guide/topics/ui/custom-components.html

+0

我需要把一些信息放到單選按鈕中(自定義屬性)。 – Oyeme

+0

感謝您的鏈接!它是我需要知道的。 – Oyeme