2017-03-07 36 views
0

我有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時,它將取消選中第一個按鈕並檢查第二個按鈕並獲取第二個按鈕的值。

This is my problem, when click other radiobutton it not unchecked others button

+0

RadioGroup應該在activity_main.xml中,並且將單選按鈕充氣一個,並將它們添加到RadioGroup – Alan

+0

仍然相同..可以選擇多個然後一個 –

+0

請檢查我的答案,它應該是更多的細節;並隨時留下評論 – Alan

回答

1

嘗試下面的代碼,

public class MainActivity extends AppCompatActivity { 

LinearLayout layoutRadioNotes; 
ArrayList<String> noteArray = new ArrayList<>(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    layoutRadioNotes = (LinearLayout) findViewById(R.id.layoutRadioNotes) ; 
    noteArray.add("test 1"); 
    noteArray.add("test 1"); 
    showRadioNote(); 
} 

void showRadioNote(){ 
    layoutRadioNotes.removeAllViews(); 
    if(noteArray.size() != 0){ 
     final View view = View.inflate(this, R.layout.radio_button, null); 
     layoutRadioNotes.addView(view); 

     final RadioGroup radioGroupNotes = (RadioGroup) view.findViewById(R.id.radioGroupNotes); 
     radioGroupNotes.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       // checkedId is the RadioButton selected 
      } 
     }); 
     for(int i=0;i<noteArray.size();i++){ 
      final RadioButton radioChildNotes = new RadioButton(this); 
      radioChildNotes.setText(noteArray.get(i)); 
      radioGroupNotes.addView(radioChildNotes); 

     } 
    } 
} 

}

activity_main.xml中

<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layoutRadioNotes" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.moduletest.MainActivity"> 


</LinearLayout> 

你的按鍵佈局,

<?xml version="1.0" encoding="utf-8"?> 

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/radioGroupNotes" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
+0

仍然不能..我的數據只顯示一個radioButton –

+0

是的...它的工作..但我怎麼能得到價值文本radioButton當我點擊它? –

+0

爲你添加一個聽衆編輯答案:) – sadat

相關問題