我有3個文件。第一個MainActivity.java,第二個是activity_main.xml,第三個是radio_button.xml。在充氣視圖後識別基於數據庫的id單選按鈕android
這是radio_button.xml。我在這個文件中創建radioGroup和radioButton。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/radioGroupNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioChildNotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No respond to phone call"
android:checked="false" />
</RadioGroup>
</LinearLayout>
這是activity_main。我在這個xml中創建了LinearLayout。因爲我想在這個LinearLayout中膨脹radio_button.xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutRadioNotes"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</LinearLayout>
這是MainActivity.java。我只是把主要功能只在這個文件中。
void showRadioNote(){
layoutRadioNotes.removeAllViews();
if(noteArray.size() != 0){
for(int i=0;i<noteArray.size();i++){
final View view = View.inflate(this, R.layout.radio_button, null);
layoutRadioNotes.addView(view);
final RadioGroup radioGroupNotes = (RadioGroup) view.findViewById(R.id.radioGroupNotes);
final RadioButton radioChildNotes = (RadioButton) view.findViewById(R.id.radioChildNotes);
radioChildNotes.setText(noteArray.get(i));
}
}
}
我已經可以在noteArray中顯示所有數據。但問題是,如果我點擊所有radioButton,它會檢查所有。我想讓它可以只選擇一個,意思是如果我點擊第一個radioButton,那麼它將檢查firstButton並獲得值firstbutton,然後當我單擊第二個radioButton時,它將取消選中第一個按鈕並檢查第二個按鈕並獲取第二個按鈕的值。
RadioGroup應該在activity_main.xml中,並且將單選按鈕充氣一個,並將它們添加到RadioGroup – Alan
仍然相同..可以選擇多個然後一個 –
請檢查我的答案,它應該是更多的細節;並隨時留下評論 – Alan