2017-03-07 33 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

相关问题