Android笔记——单选框(RadioButton)
作者:互联网
一、常见样式:
android:button="@null" : 去掉单选框前面的圆框
二、RadioButton控件
<RadioGroup android:id="ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="对齐方式(水平或者垂直)"> <RadioButton android:id="ID1" android:layout_width="宽度" android:layout_height="高度" android:text="文本1" android:checked="true"/> <RadioButton android:id="ID2" android:layout_width="宽度" android:layout_height="高度" android:text="文本2"/> ...... </RadioGroup>
三、单选组添加事件
获取的按钮组.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) group.findViewById(checkedId); Toast.makeText(MainActivity.this,radioButton.getText(),Toast.LENGTH_SHORT).show(); } });
四、练习
(1)布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="15dp"> <RadioGroup android:id="@+id/group01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/radio01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:textSize="18sp" android:checked="true" android:textColor="#f47920"/> <RadioButton android:id="@+id/radio02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textSize="18sp" android:textColor="#f47920"/> </RadioGroup> <RadioGroup android:layout_marginTop="15dp" android:layout_below="@+id/group01" android:id="@+id/group02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radio03" android:layout_width="60dp" android:layout_height="30dp" android:background="@drawable/bg" android:checked="true" android:gravity="center" android:text="男" android:textColor="#000" android:textSize="18sp" android:button="@null"/> <RadioButton android:id="@+id/radio04" android:layout_width="60dp" android:layout_height="30dp" android:text="女" android:textSize="18sp" android:textColor="#000" android:background="@drawable/bg" android:gravity="center" android:button="@null" android:layout_marginLeft="15dp"/> </RadioGroup> </RelativeLayout>
(2)drawable文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true"> <shape> <solid android:color="#1d953f"/> <corners android:radius="15dp"/> </shape> </item> <item android:state_pressed="false"> <shape> <solid android:color="#ffffff"/> <corners android:radius="15dp"/> </shape> </item> </selector>
(3)activity文件
package com.example.lqh.firstproctect; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity extends Activity { private RadioGroup rg01; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg01 = (RadioGroup) findViewById(R.id.group01); rg01.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radioButton = (RadioButton) group.findViewById(checkedId); Toast.makeText(MainActivity.this,radioButton.getText(),Toast.LENGTH_SHORT).show(); } }); } }
(4)效果图
标签:Toast,group,单选框,RadioButton,RadioGroup,import,Android,android 来源: https://www.cnblogs.com/lqh0904/p/15315028.html