本文实例为大家分享了Android控件RadioButton的使用代码,供大家参考,具体内容如下
内容
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="match_parent" tools:context=".RadioActivity"> <RadioGroup //定义一个单选按钮组 android:id="@+id/rg_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton //单选按钮一 使用默认样式 android:id="@+id/rb_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" android:textSize="24sp" android:textColor="@color/black"/> <RadioButton //单选按钮2 使用默认样式 android:id="@+id/rb_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textSize="24sp" android:textColor="@color/black"/> </RadioGroup> <RadioGroup //组2 android:id="@+id/rg_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/rg_1" android:layout_marginTop="50dp"> <RadioButton android:layout_width="50dp" android:layout_height="wrap_content" android:text="男" android:button="@null" //无按钮样式 android:textSize="24sp" android:background="@drawable/selector_radiobutton" //自定义背景 android:textColor="@color/black" android:checked="true" android:gravity="center"/> <RadioButton android:layout_width="50dp" android:layout_height="wrap_content" android:gravity="center" android:button="@null" //无按钮样式 android:text="女" android:background="@drawable/selector_radiobutton" //自定义背景 android:textSize="24sp" android:textColor="@color/black"/> </RadioGroup> </RelativeLayout>
//selector_radiobutton.xml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true"> //单选被选中的样式 <shape android:shape="rectangle"> <solid android:color="#ff66ff"/> <corners android:radius="5dp"/> </shape> </item> <item android:state_checked="false"> //单选没被选中的样式 <shape android:shape="rectangle"> <stroke android:color="#cc33ff" android:width="2dp"/> <corners android:radius="5dp"/> </shape> </item> </selector>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class RadioActivity extends AppCompatActivity { private RadioGroup rg_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio); rg_1 = findViewById(R.id.rg_1); rg_1.setOnCheckedChangeListener((group, checkedId) -> {//设置组中单选按钮选中事件 RadioButton radioButton = findViewById(checkedId);//获取被选中的id Toast.makeText(RadioActivity.this,radioButton.getText(),Toast.LENGTH_SHORT) .show();//吐司一下被选中的文本值 }); } }
运行效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是闪闪果汁最近收集整理的关于Android控件RadioButton的使用方法的全部内容,更多相关Android控件RadioButton内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复