对于单选按钮相信每个人都十分熟悉,这类控件也已经接触了很多了。不过在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);}});}
这两个实例源码都较为简单,就不给了



更多相关文章

  1. Android4.4系统源代码百度网盘下载
  2. android软键盘事件处理
  3. MVVM在Android中的初学之路
  4. 第十四周实验报告:实验四 Android程序设计
  5. Android帧动画在应用启动时同步启动
  6. Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配
  7. Android(安卓)中的ORM框架
  8. Android(安卓)锁屏功能
  9. 操作 Android(安卓)模拟器

随机推荐

  1. android实现关键字搜索功能
  2. android开发—01开发环境的搭建
  3. 【Android】Android蓝牙开发深入解析
  4. android各版本的差异
  5. Android(安卓)剪切板监听
  6. 查看基于Android(安卓)系统单个进程内存
  7. Android从SD卡和Res读取图片,防止发生OOM
  8. How to Use Android(安卓)Downloads Prov
  9. Android
  10. [置顶] [Android] AsyncTask使用实例---