Android常用控件(二)

选择类控件

一、RadioButton单选控件

1、RadioButton的相关属性


RadioButton继承Button,Button继承TextView,所以RadioButton具有TextView具有的属性。

下面表示一个单选按钮组,当其中一个RadioButton被选中时候,其他RadioButton会变为位选中状态,即同一时间,只有一个RadioButton为checked状态

<RadioButtonGroup>
<RadioButton />
<RadioButton />
<RadioButton />
</RadioButtonGroup>
<?xml version="1.0" encoding="utf-8"?>
<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">

    <RelativeLayout
        android:id="@+id/name_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dp"
            android:padding="5dp"
            android:text="帐号:"
            android:textSize="24sp" />

        <Button
            android:id="@+id/btn"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/btn"
            android:layout_toRightOf="@id/text" />


    </RelativeLayout>
</RelativeLayout>

2、RadioButton相关的监听事件

通过设置RadioButton所在的RadioGroup设置选择状态监听,RadioGroup.setOnCheckedChangeListener(),重写其中的onCheckedChanged()方法


package com.songjialongs.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * Created by songjialongs on 2016/5/25.
 */
public class RadioActivity extends AppCompatActivity {
    private RadioGroup rg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);
        rg = (RadioGroup) findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {//iint i为被选中的RadioButton,即状态为checked的RadioButton
                    case R.id.rtn1:
                        //=========通过RadioGroup.getChildAt(int n)获取RadioButton实例,int n与布局文件中RadioButton排列顺序有关
                        rg.getChildAt(0);
                        break;
                    case R.id.rtn2:
                        //==========Toast提示框 参数列表分别为context,String(提示信息),显示模式(LENGTH_SHORT为短暂显示)
                        Toast.makeText(RadioActivity.this, "选项2被选中", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rtn3:
                        //=========SnackBar提示框 参数列表分别为View,String,显示模式
                        Snackbar.make(radioGroup, "选项3被选中", Snackbar.LENGTH_INDEFINITE).show();
                        break;
                }
            }
        });
    }
}

3、自定义RadioButton

(1)Res/drawable文件夹下新建selector.xml选择器文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/Image_Checked" android:state_checked="true" /><!--当按钮被选中时-->
    <item android:drawable="@drawable/Image" /><!--按钮未被选中时-->
</selector>
(2)Layout文件修改RadioButton的button属性
 <RadioButton
            android:id="@+id/rtn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            
            android:button="@drawable/radio_button"
            android:text="选项1" />
            <!--@drawable/radio_button为上面设定的selector文件-->
(3)完成 

二、CheckBox 多选控件

 

1、CheckBox相关属性

CheckBox 继承Button,Button继承TextView,所以RadioButton具有TextView具有的属性。
.isChecked() 方法返回一个布尔值,表示按钮是否被选中

2、自定义CheckBox控件

与上面RadioButton同理

3、CheckBox相关的事件处理

与上面RadioButton同理


三、ToggleButton;Switch 开关控件

1.相关属性

textOn 状态为“ON”时开关按钮显示的文本
textOff 状态为“OFF”时开关显示的文本
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值