实现以下效果,当选择单选框1时,单选框上方显示【我选择的是radio1】,当选择单选框2时,单选框上方显示【我选择的是radio2】,要求写出布局文件核心代码和Activity中的按钮点击事件监听方法。
时间: 2024-03-01 09:49:44 浏览: 9
布局文件核心代码:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio 1"/>
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio 2"/>
</RadioGroup>
<TextView
android:id="@+id/result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
```
Activity中按钮点击事件监听方法:
```
private RadioGroup radioGroup;
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radio_group);
resultText = findViewById(R.id.result_text);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton radioButton = findViewById(checkedId);
resultText.setText("我选择的是" + radioButton.getText().toString());
}
});
}
```