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实现业务逻辑。

更多相关文章

  1. Android设置显示文本
  2. android限制文本长度
  3. Android TextView多行文本滚动实现
  4. android设置EditText不可编辑内容,响应点击事件
  5. 求助: Android 加载 webview, 点击webview 中网页时间, 如何触发
  6. Android 在界面中显示以及输入文本信息 TextView和EditText
  7. android之实现各个组件点击事件处理
  8. android 处理鼠标滚轮事件
  9. Android中设置文本颜色的三种方法

随机推荐

  1. AutoCompleteTextView 无限制输入字符及
  2. android:transcriptMode用法
  3. webkit里网页调用android的方法(函数)
  4. android 内核编程
  5. Android学习之Intent实现页面跳转
  6. 定制android通知(Notification)
  7. Android(4.Activity的基本控件)
  8. Binder驱动之内存映射----binder_mmap
  9. 说说在 Android(安卓)中如何发送 HTTP 请
  10. 初识Activity