RadioButton 单选按钮 实现单选功能
package com.example.kn_day04_2radiobutton;
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener;
/**  * RadioButton单选按钮  * 实现单选功能  * @author KNOWN  *  */ public class MainActivity extends Activity {
 /*   * RadioButton单选按钮   *   * 要想让几个radiobutton之间具备单选效果   * 必须将他们RadioButton包裹在同一个RadioGroup中   *   * */  RadioGroup rg;  @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);     rg = (RadioGroup)findViewById(R.id.radiogroup1);   Button btn1 = (Button)findViewById(R.id.btn1);   Button btn2 = (Button)findViewById(R.id.btn2);       //设置默认选中项,参数对应被选择的RadioButton的id   rg.check(R.id.rb1);       //设置RadioGroup选项切换时的响应事件   rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {         /*     * group代表当前的RadioGroup对象     * checkedId代表当前被选中的RadioButton的id     * 当id值为-1的时候,代表没有被选择的选项     * */    @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {     // TODO Auto-generated method stub         if( checkedId!=-1){      RadioButton rb = (RadioButton)findViewById(checkedId);      Log.i("改变后选中的是:", rb.getText().toString());     }else {      Log.i("改变后选中的是:", "无");     }    }   });         /**    * 获取用户选择值    */   btn1.setOnClickListener(new OnClickListener() {        @Override    public void onClick(View v) {         if( rg.getCheckedRadioButtonId()!=-1){      RadioButton rb = (RadioButton)findViewById(rg.getCheckedRadioButtonId());      Log.i("当前选中的是:", rb.getText().toString());     }    }   });   /**    * 清空选中项    */   btn2.setOnClickListener(new OnClickListener() {        @Override    public void onClick(View v) {      //清空选中项     rg.clearCheck();     Log.i("当前选中的是:", "无");    }   });    } } ******** 对应布局文件acvity_maiin.xml
    xmlns:tools=" http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.kn_day04_2radiobutton.MainActivity" >

             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:textSize="28sp"
        android:text="请选择你的角色" />
    
             android:layout_width="match_parent"
        android:layout_height="wrap_content"
         
         android:orientation="horizontal"        
         android:checkedButton="@+id/rb1"
        
        android:layout_below="@+id/textview1"
        android:layout_alignLeft="@+id/textview1"
        android:layout_alignStart="@+id/textview1"
        android:layout_marginTop="30dp"
        >
        
                     android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            android:text="法师"
            />
                     android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            android:text="圣骑士"
            />
                     android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="28sp"
            android:text="野蛮人"
            />
    

             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_below="@+id/radiogroup1"
        android:layout_marginTop="30dp"
        android:layout_alignLeft="@+id/radiogroup1"
        android:layout_alignStart="@+id/radiogroup1"
        android:textSize="28sp"
        android:text="获取选中的角色"/>
             android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_toRightOf="@+id/btn1"
        android:layout_toEndOf="@+id/btn1"
        android:layout_alignBaseline="@+id/btn1"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:textSize="28sp"
        android:text="清楚选中的角色"/>

更多相关文章

  1. Android 单选按钮Radio的使用
  2. Android下图片或按钮等可拖动到任意位置的效果实现源码
  3. android 实现透明按钮
  4. Android RadioGroup RadioButton 自定义样式------按钮式
  5. Android之Button按钮
  6. 窗体两个按钮各占一半
  7. 自定义单选按钮(RadioButton)的样式
  8. Android按钮控件之RadioGroup和RadioButton
  9. android单选按钮RadioGroup的详细使用

随机推荐

  1. 绝对让你理解Android中的Context
  2. 一些 Android(安卓)小知识点的总结
  3. Android(安卓)Handler使用的安全问题
  4. android 最简单的圆角阴影效果
  5. eclipse下android工程目录讲解
  6. 用eclipse编写Android程序时怎样生成apk
  7. Android开源项目--工具库篇
  8. Android地图获取详细街道地址信息 精确定
  9. Android(安卓)NDK系列三(Android(安卓)Stu
  10. 打造Android万能上拉下拉刷新框架--XRefr