在膨胀视图后根据数据库识别 id 单选按钮 android

Identify id radio button based on database after inflate view android

我有 3 个文件。第一个 MainActivity.java,第二个 activity_main.xml,第三个 radio_button.xml。

这是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 中显示所有数据了。但问题是如果我单击所有单选按钮,它会检查所有。我想让它只能是 select 一个,意思是如果我点击第一个 radioButton ,那么它会检查 firstButton 并获取 firstbutton 的值,然后当我点击第二个 radioButton 时,它将取消选中 firstbutton 并检查第二个按钮并获取值对于第二个按钮。

试试下面的代码,

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" />