Android Button

选择按钮在开发过程中也是使用频率非常高的控件. 但是我发现很多人都是一知半解, 总结下Android选择按钮控件;

记得活用目录功能

包括以下控件:

  • CheckBox 多选框
  • RadioButton 单选框
  • Switch 开关
  • ToggleButton 切换按钮

CompoundButton

继承关系

java.lang.Object ↳ android.view.View ↳ android.widget.TextView ↳ android.widget.Button ↳ android.widget.CompoundButton 直接继承CompoundButton的子类 CheckBox, RadioButton, Switch, SwitchCompat, ToggleButton

可以看出所有选择的控件都继承自CompoundButton这个类. 其实他的几个子类都没有新增多少方法. 都是使用的该类的方法和属性.

该类是抽象类, CompoundButton并不能直接写在布局文件中. 他是所有选择按钮的父类. 几个常用的方法都是继承自该类.

属性介绍

比起他的父类Button其实也就新增了四个布局属性.

属性 描述
android:button 设置一张图片来作为显示
android:buttonTint 渲染颜色
android:buttonTintMode 渲染模式
android:checked 设置为选中状态

方法介绍

用来重写进行自定义控件的方法我不够熟悉我就不讲了.

设置选择状态

可以通过设置参数在选中和未选中的状态之间切换.

void setChecked (boolean checked)

判断是否被选择

boolean isChecked ()

设置按钮的图片

等用于属性android:button, 设置按钮的图片显示. 以下我用RadioButton作为示例

void setButtonDrawable (Drawable drawable) // 设置按钮显示

void setButtonDrawable (int resId) // 资源id设置按钮图片

Drawable getButtonDrawable () // 得到按钮的图片

设置渲染色彩

给按钮加上滤镜一样的色彩遮盖. 同样之前我在属性里面也介绍过了. 这需要同时调用两个方法:

  1. 渲染颜色
  2. 渲染模式

设置渲染颜色

void setButtonTintList (ColorStateList tint)

设置渲染模式

void setButtonTintMode (PorterDuff.Mode tintMode)

选择状态改变监听器

该方法是最常用的.

void setOnCheckedChangeListener (CompoundButton.OnCheckedChangeListener listener)

切换当前选择状态

和setChecked不同的是他只能从选中, 不能取消选中.

void toggle ()

获取Padding值

int getCompoundPaddingLeft ()

int getCompoundPaddingRight ()

模拟点击

该方法是View就有的. 手动模拟用户点击事件

boolean performClick ()

CheckBox

多选框控件, 这个控件没什么好讲的. 相对父类CompoundButton没有新增任何方法. 主要就是提供选择的状态变化. 可以看做最基本的选择控件.

使用方法

直接在布局文件中写即可

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="设计师吴彦祖"
        />

效果图:

RadioButton

这是一个单选按钮的控件对象.

使用起来很简单:

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton"/>

RadioGroup

既然是单选, 那肯定不止一个选择按钮吧. 这里就要涉及到另一个类RadioGroup了. 只有被RadioGroup包括的RadioButton之间才存在单选的关系.

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="horizontal"
        android:checkedButton="@id/radioButton3"
        >

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton"/>

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton"/>

    </RadioGroup>

可以看到RadioGroup使用了两个属性:

android:orientation="horizontal"	// 设置内部的按钮是水平还是垂直排列
android:checkedButton="@id/radioButton3" // 设置默认被选中的按钮是那个

方法介绍

添加View

添加一个子View到RadioGroup的容器中.

void addView (View child,  // 视图内容
                int index,  // 索引位置
                ViewGroup.LayoutParams params) // 设置添加进来的View的布局参数
手动单选
void check (int id) // RadioButton的资源id
清除所有选择
void clearCheck ()
设置一个新的布局参数
RadioGroup.LayoutParams generateLayoutParams (AttributeSet attrs)
被选中RadioButtonId

得到当前被选中的RadioButton. 如果没有任何RadioButton被选中返回-1

int getCheckedRadioButtonId ()
选择状态改变监听器
void setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener)
层级变化监听器

在RadioGroup容器添加或者删除子控件的时候回调该监听器

void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)

ToggleButton

void	setTextOff(CharSequence textOff)
void	setTextOn(CharSequence textOn)

CharSequence	getTextOff()
CharSequence	getTextOn()

void	setChecked(boolean checked)

属性

android:textOff

android:textOn

android:disabledAlpha

由于继承自Button同样可以使用checkChangeListener监听按钮选择

mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

  }
});

或者使用TextChangedListener监听文字变化

mToggleButton.addTextChangedListener(new TextWatcher() {
  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  }

  @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
  }

  @Override public void afterTextChanged(Editable s) {
  }
});

Switch

切换开关按钮. 类似ToggleButton. 但是拥有平滑切花动画. 支持自定义样式.

Tip: 圆形是按钮, 圆角长方形是轨道.

api v21 才能使用的属性

android:showText // 在按钮上面显示文本内容: (开启/关闭)

android:splitTrack // 轨道和按钮分开(即不会重叠在一起)


Tip: showTextsetText方法显示的文本是不同区域的. 非同一个.

文本内容自定义

android:switchMinWidth // 轨道的长度

android:switchPadding // Switch和文本内容(setText)的间距

android:switchTextAppearance // showText 的文本样式

// 开启和关闭状态下显示的文本, 如果没有启用showText无效
android:textOff
android:textOn

按钮图标自定义

android:thumb // 按钮图标

android:thumbTextPadding // 如果使用了thumb, 可以控制showText的文本边距

android:thumbTint // 按钮着色

android:thumbTintMode // 着色模式

Tip: Thumb不能引用颜色属性(否则不显示), 如果指定普通Drawable情况Switch的On和Off显示的按钮都是一样. 可以通过指定selector来控制切换按钮.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/ic_sun"/>
    <item android:drawable="@drawable/ic_moon"/>
</selector>

轨道自定义

android:track

android:trackTint

android:trackTintMode

自定义Track的高度和宽度都受到Thumb的限制.

代码的功能XML属性全部都实现

猜你喜欢

转载自juejin.im/post/5b0e8778f265da08d057b3db
今日推荐