中级控件——单选按钮RadioButton——horizontal方向

单选按钮要在一组按钮中选择其中一项,并且不能多选,这要求有个容器确定这组按钮的范围,这个容器便是单选组RadioGroup。

RadioGroup实质上是个布局,同一组RadioButton都要放在同一个RadioGroup节点下。

除了RadioButton,也允许放置其他控件。

单选组与线性布局相比,它们主要有以下两个区别:

(1)单选组多了管理单选按钮的功能,而线性布局不具备该功能;

(2)如果不指定orientation属性,那么单选组默认垂直排列,而线性布局默认水平排列;

判断选中了哪个单选按钮,通常不是监听某个单选按钮,而是监听单选组的选中事件。

下面是RadioGroup常用的3个方法:

(1)、check:选中指定资源编号的单选按钮。(2)、getCheckedRadioButtonId:获取选中状态单选按钮的资源编号。(3)、setOnCheckedChangeListener:设置单选按钮勾选变化的监听器。

==================================================================================

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选择您的性别"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <RadioGroup
        android:id="@+id/rg_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rb_male"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="false"
            android:text="男"
            android:textColor="@color/black"
            android:textSize="17sp" />

        <RadioButton
            android:id="@+id/rb_female"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="false"
            android:text="女"
            android:textColor="@color/black"
            android:textSize="17sp" />
    </RadioGroup>

    <TextView
        android:id="@+id/tv_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

 

package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener
{
    private TextView tv_sex; // 声明一个文本视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        // 从布局文件中获取名叫tv_sex的文本视图
        tv_sex = findViewById(R.id.tv_sex);

        // 从布局文件中获取名叫rg_sex的单选组
        RadioGroup rg_sex = findViewById(R.id.rg_sex);

        // 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法
        rg_sex.setOnCheckedChangeListener(this);

        // 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法
//        rg_sex.setOnCheckedChangeListener(new RadioListener());
    }

    // 在用户点击组内的单选按钮时触发
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId)
    {
        if (checkedId == R.id.rb_male)
        {
            tv_sex.setText("哇哦,你是个帅气的男孩");

        } else if (checkedId == R.id.rb_female)
        {
            tv_sex.setText("哇哦,你是个漂亮的女孩");
        }
    }

    // 定义一个单选监听器,它实现了接口RadioGroup.OnCheckedChangeListener
//    class RadioListener implements RadioGroup.OnCheckedChangeListener
//    {
//        // 在用户点击组内的单选按钮时触发
//        @Override
//        public void onCheckedChanged(RadioGroup group, int checkedId)
//        {
//            Toast.makeText(MainActivity.this, "您选中了控件"+checkedId, Toast.LENGTH_LONG).show();
//        }
//    }
}

 

 

 

==========================================================================================


package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
{
private TextView tv_sex; // 声明一个文本视图对象

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


// 从布局文件中获取名叫tv_sex的文本视图
tv_sex = findViewById(R.id.tv_sex);

// 从布局文件中获取名叫rg_sex的单选组
RadioGroup rg_sex = findViewById(R.id.rg_sex);

// 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法
// rg_sex.setOnCheckedChangeListener(this);

// 设置单选监听器,一旦点击组内的单选按钮,就触发监听器的onCheckedChanged方法
rg_sex.setOnCheckedChangeListener(new RadioListener());
}



// 定义一个单选监听器,它实现了接口RadioGroup.OnCheckedChangeListener
class RadioListener implements RadioGroup.OnCheckedChangeListener
{
// 在用户点击组内的单选按钮时触发
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
Toast.makeText(MainActivity.this, "您选中了控件"+checkedId, Toast.LENGTH_LONG).show();
}
}
}



 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值