Android RadioButton单选按钮
封面图
•
2025-10-03
•
Android开发
166
0
0
0
在 Android 开发中,`RadioButton` 是一种用于让用户从多个选项中选择一个的 UI 控件。它通常与 `RadioGroup` 一起使用,以确保同一组中的多个 `RadioButton` 中只能选一个。 --- ## ✅ 一、基本用法 ### 1. 在 XML 布局文件中添加 `RadioGroup` 和 `RadioButton` ```xml <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项3" /> </RadioGroup> ``` --- ### 2. 在 Java/Kotlin 中获取并监听选中状态 #### Java 示例: ```java RadioGroup radioGroup = findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton selected = group.findViewById(checkedId); if (selected != null) { Toast.makeText(MainActivity.this, "你选择了: " + selected.getText(), Toast.LENGTH_SHORT).show(); } } }); ``` #### Kotlin 示例: ```kotlin val radioGroup = findViewById<RadioGroup>(R.id.radioGroup) radioGroup.setOnCheckedChangeListener { group, checkedId -> val selected = group.findViewById<RadioButton>(checkedId) selected?.let { Toast.makeText(this, "你选择了: ${it.text}", Toast.LENGTH_SHORT).show() } } ``` --- ## ✅ 二、设置默认选中项 你可以通过代码或 XML 设置默认选中的 `RadioButton`。 ### 在 XML 中设置默认选中: ```xml <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项1" android:checked="true" /> ``` ### 在代码中设置默认选中: ```java radioGroup.check(R.id.radioButton1); // 选中 ID 为 radioButton1 的 RadioButton ``` --- ## ✅ 三、获取当前选中的值 ```java int selectedId = radioGroup.getCheckedRadioButtonId(); if (selectedId != -1) { RadioButton selected = findViewById(selectedId); String selectedText = selected.getText().toString(); } ``` --- ## ✅ 四、注意事项 - `RadioGroup` 只能包含 `RadioButton`。 - 如果你想要自定义样式(如圆角、颜色等),可以使用 `android:buttonTint` 或自定义 `RadioButton` 的背景。 - `RadioButton` 支持 `android:enabled="false"` 来禁用某个选项。 --- ## ✅ 五、示例:完整布局 + 逻辑 ### 布局文件(activity_main.xml): ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项1" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项2" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项3" /> </RadioGroup> </LinearLayout> ``` ### Java 代码(MainActivity.java): ```java import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup radioGroup = findViewById(R.id.radioGroup); radioGroup.setOnCheckedChangeListener((group, checkedId) -> { RadioButton selected = group.findViewById(checkedId); if (selected != null) { Toast.makeText(this, "你选择了: " + selected.getText(), Toast.LENGTH_SHORT).show(); } }); } } ``` --- 如果你需要更高级的功能(比如动态添加按钮、自定义样式、与 ViewModel 集成等),也可以告诉我,我可以提供更详细的指导!
上一篇:
android Spinner控件详解
下一篇:
Android API之CheckedTextView代码演示
标题录入,一次不能超过6条
返回顶部
留言
留言
评论