我爲Radio Button創建了一個自定義視圖像這樣。 如何從自定義視圖中獲取RadioButton的值,並且一次只想選擇一個單選按鈕。Android:從自定義視圖中獲取RadioButton的價值
public void getData(){
cappingLayout.removeAllViews();
for (Capping capping: productList.getCappingList()){
if (getActivity()!=null) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.product_capping, cappingLayout, false);
cappingRadio = (RadioButton) view.findViewById(R.id.fragment_product_detail_capping);
cappingRadio.setText(capping.getCapping_type());
cappingLayout.addView(view);
}
}
}
Product_capping.xml
<RadioButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_product_detail_capping"
style="@style/Widget.AppCompat.CompoundButton.CheckBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
tools:text="Normal"/>
我想點擊該按鈕後能得到單選按鈕的值。
通過值,你的意思是與單選按鈕相關的文字? –
是的。我已經從服務器檢索數據,但無法獲取按鈕單擊事件 –
上已選中的radioButton的值,您可以使用'radioButton.isChecked'來查看它是否被選中,或者'radioButton.getText'來獲取內容,在下面描述單選按鈕 –