Android(安卓)UI控件之RadioGroup、RadioButton
16lz
2021-01-24
对于单选按钮相信每个人都十分熟悉,这类控件也已经接触了很多了。不过在Android是如
和实现的呢?又是如何对单选按钮进行监听的呢?
以一个例子程序为例:
首先看常规的单选按钮:
布局文件的代码如下:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RadioButton、RadioGroup练习" /> <RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:text="Android程序设计" android:textAlignment="viewEnd" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider" /> <RadioButton android:text="Java程序设计" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RadioGroup>其次再看看特别一点的.单选按钮自定义:
布局文件的代码如下:
<RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:text="Android程序设计" android:textAlignment="viewEnd" android:button="@null" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@android:drawable/btn_star" /> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider" /> <RadioButton android:text="Java程序设计" android:button="@null" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@android:drawable/btn_star" /> </RadioGroup>不过有时候我们有这样一种需求,就是按钮是在右边的如何实现呢?
布局文件代码部分
<RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:text="Android程序设计" android:button="@null" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableRight="@android:drawable/btn_star" /> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider" /> <RadioButton android:text="Java程序设计" android:button="@null" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableRight="@android:drawable/btn_star" /> </RadioGroup>
以上是RadioButton的三种效果,那么RadioButton又是如何实现监听的呢?
看代码部分:
private RadioGroup group =null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);group = (RadioGroup)findViewById(R.id.group);group.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stub System.out.println(checkedId);}});}这两个实例源码都较为简单,就不给了
更多相关文章
- Android4.4系统源代码百度网盘下载
- android软键盘事件处理
- MVVM在Android中的初学之路
- 第十四周实验报告:实验四 Android程序设计
- Android帧动画在应用启动时同步启动
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配
- Android(安卓)中的ORM框架
- Android(安卓)锁屏功能
- 操作 Android(安卓)模拟器