zoukankan      html  css  js  c++  java
  • 使用RadioGroup与RadioButton实现多选一

    RadioGroup是RadioButton的集合,
    RadioGroup里面可以包含很多RadioButton,提供多选一机制,只能选择其中一个

    RadioGroup的orientation(方向)有两个属性
    android:orientation = {"vertial" ---垂直分布
                 {"horizontal" ---水平排布

    注意RadioGroup一般用于单选,而且选中之后不能取消

    而CheckBox一般用于多选,而且选中之后可以取消

    例程:

    1:主程序:

    package com.Thinker.checkbox;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    
    public class MainActivity extends Activity implements OnCheckedChangeListener{
        private RadioGroup radioGroup;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            /**
             * 找到相应控件对象赋给这个引用
             */
            radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
            /**
             * 设置监听事件
             */
            radioGroup.setOnCheckedChangeListener(this);
        }
        @Override
        public void onCheckedChanged(RadioGroup arg0, int checkedId) {
            // TODO Auto-generated method stub
            //checkedId是所选所监控到的RadioGroup中的RadioButton的id
            switch(checkedId){
            case R.id.radio0:
                Log.i("tag", "你确定你是男生?");
                break;
            case R.id.radio1: 
                Log.i("tag", "你确定你是女生");
                break;
            case R.id.radio2: 
                Log.i("tag", "再想想你到底是男生还是女生");
                break;
                default:
                    break;
            }
        }
        
    }

    2:控件属性

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        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=".MainActivity" >
    
        <!-- 注意checkbox选中之后还可以进行取消 -->
    
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="14dp" >
    
            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="男" />
    
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女" />
    
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="其他" />
        </RadioGroup>
    
    </RelativeLayout>
  • 相关阅读:
    solr7之solrJ的使用
    solr7.3.1在CentOS7上的安装
    nginx配置:location配置方法及实例详解
    [读书]10g/11g编程艺术深入体现结构学习笔记(持续更新...)
    liunx系统计划任务管理(at/crond调度)
    Golden Gate 概念和机制
    Oracle三大经典表连接适用情况
    Oracle索引简单介绍与示例
    Oracle RAC的日志体系
    Oracle10g RAC的简单操作
  • 原文地址:https://www.cnblogs.com/rain-1/p/5149216.html
Copyright © 2011-2022 走看看