RadioGroup管理一组RadioButton,其中的RadioButton只能做出单一选择。

效果如:

先给出整个Activity的布局文件main.xml:

xml version="1.0" encoding="UTF-8"?><<>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <<>TextView android:id="@+id/radiobutton_textview" android:layout_width="fill_parent" android:layout_height="50dip" android:textSize="18dip" android:textStyle="bold" android:background="@android:drawable/title_bar" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" /> <<>RadioGroup android:id="@+id/group" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <<>RadioButton android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="50dip" android:textSize="20dip" android:paddingLeft="30dip" android:text="Android新手" android:button="@null" android:drawableRight="@android:drawable/btn_radio"/> <<>View android:layout_width="fill_parent" android:layout_height="1px" android:background="?android:attr/listDivider" /> <<>RadioButton android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="50dip" android:textSize="20dip" android:paddingLeft="30dip" android:text="Android高手" android:button="@null" android:drawableRight="@android:drawable/btn_radio"/> RadioGroup> LinearLayout>
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>

RadioButton 默认按钮在文本的左边,我放到右边,涉及代码:android:button="@null",将左侧的按钮消除;

android:drawableRight="@android:drawable/btn_radio",在文本右侧添加按钮。

MainActivity主要介绍 单选按钮组监听事件:OnCheckedChangeListener

   1:  /**
   2:   * @
   3:   * @author ahutzh
   4:   * @data 2011-4-14
   5:   */
   6:  public class MainActivity extends Activity {
   7:  
   8:      private TextView textView;
   9:      private RadioGroup group;
  10:  
  11:      /** Called when the activity is first created. */
  12:      @Override
  13:      public void onCreate(Bundle savedInstanceState) {
  14:          super.onCreate(savedInstanceState);
  15:          setContentView(R.layout.main);
  16:  
  17:          textView = (TextView) findViewById(R.id.radiobutton_textview);
  18:          group = (RadioGroup) findViewById(R.id.group);
  19:  
  20:          // 单选按钮组监听事件
  21:          group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  22:  
  23:              @Override
  24:              public void onCheckedChanged(RadioGroup group, int checkedId) {
  25:                  // 根据ID判断选择的按钮
  26:                  if (checkedId == R.id.button1) {
  27:                      textView.setText("Android新手");
  28:                  } else {
  29:                      textView.setText("Android高手");
  30:                  }
  31:              }
  32:          });
  33:      }
  34:  }
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
RadioGroup注册监听事件OnCheckedChangeListener(),在onCheckedChanged实现业务逻辑。

更多相关文章

  1. android 获取控件真实高度
  2. [置顶] Android(安卓)View系统学习文章汇总
  3. Android中事件分发机制分析
  4. 菜鸟学Android开发系列之:初探Button和TextView
  5. android中自定义RadioButton
  6. Android解析XML-详尽
  7. android简单学习总结
  8. 拖动条SeekBar的简单使用
  9. 【android】Android(安卓)SDK 配置

随机推荐

  1. Android梳理 Fragment
  2. Android(安卓)仿微信Activity左右切换 【
  3. [置顶] Android——4.2.2 文件系统目录分
  4. Android各版本源码下载链接
  5. Android实现微信分享及注意事项
  6. Android的Task和Activity相关
  7. Android(安卓)App安装失败错误总结
  8. android中的dispatchTouchEvent、onInter
  9. Android(安卓)简单联系人操作
  10. Android(安卓)ormlite 框架介绍