Android studio实现单选按钮
作者:wj778
这篇文章主要为大家详细介绍了Android studio实现单选按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android studio实现单选按钮的具体代码,供大家参考,具体内容如下
创建空activity
编辑activity_main.xml文件
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/chooseTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/text1"
android:textColor="@color/colorblack"
android:textSize="30sp" />
//定义RaidGroup是要注意属性添加的位置
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:text="@string/text2"
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/text3"
</RadioGroup>
<Button
android:id="@+id/ClearBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text4" />
<Button
android:id="@+id/AddBtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/text5"
</androidx.appcompat.widget.LinearLayoutCompat>
还有strings.xml文件,代码如下:
<resources>
<string name="app_name">My App</string>
<string name="text1">我选择的是...?</string>
<string name="text2">按钮1</string>
<string name="text3">按钮2</string>
<string name="text4">清除选中</string>
<string name="text5">添加子项</string>
</resources>
再是MainActivity.java文件,代码如下: