Android RadioGroup和RadioButton使用

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

效果如:

temp_thumb1

先给出整个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实现业务逻辑。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值