我有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>
这是活动_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。我只是把 main 函数放在这个文件中。
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 ,那么它会检查 firstButton 并获取 value firstbutton,然后当我点击第二个 radioButton 时,它将取消选中 firstbutton 并检查第二个按钮并获取 secondbutton 的值。
试试下面的代码,
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);
}
}
}
}
活动_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" />
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句