使用RadioGroup与RadioButton实现

RadioGroup与RadioButton

1.RadioGroup

  RadioButton的一个集合,提供多选一机制

2.属性

  android:orientation="vertical"——垂直排布 / "horizontal"——水平排布

  决定当前的RadioGroup中RadioButton以什么形式排列

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1" >

        <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="男" />

    </RadioGroup>
</RelativeLayout>
package com.example.radiogroup;

import com.example.radiogroup.R.id;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity    implements OnCheckedChangeListener
{
    private RadioGroup rg;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rg=(RadioGroup)findViewById(R.id.radioGroup1);
        /*
         * 实现RadioGroup的监听事件
         */
        rg.setOnCheckedChangeListener(this);
    }
    @Override
    public void onCheckedChanged(RadioGroup arg0, int arg1)
    {
        // TODO Auto-generated method stub
        switch (arg1)
        {
        case R.id.radio0:
            Log.i("tag", "当前你是个男孩");
            break;
        case R.id.radio1:
            Log.i("tag", "当前你是个女孩");
            break;
        default: 
            break;
        }
    }

}

设置RadioGroup的orientation属性为”vertical”,则其中的RadioButton为垂直排列,与图片和文字没关系

RadioButton点中后再点击不能取消点中状态,而CheckBox可以

可以通过RadioGroup监听,也可以通过RadioButton监听

 

CheckBox----复选框,多选多,选择后可以再取消,
RadioButton----单选按钮,选择后不能取消
RdioGroup------单选按钮组,多选一的功能

转载于:https://www.cnblogs.com/crazyzx/articles/5242149.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值