对于Android Button,你可能并不了解

640?wx_fmt=png


今日快讯


开篇先给大家道个歉,本来在国庆前就开始向大家预告10月9号发布我自己写的文章,推出最新的LitePal 2.1.0版本,在国庆假期中我也是完成了所有的编码工作并成功将库发布到了jcenter上,还更新了LitePal项目主页的文档。本来以为万事俱备,就只等着节后来推送就可以了。

结果在昨天的公众号留言中,有一位LitePal的热衷粉丝朋友提前看到了项目主页文档的更新,并将最新版的LitePal集成到了自己的项目当中,却帮我发现了一个惊天大Bug。也确实是我自己测试疏漏,由于LitePal 2.1.0版本中对Kotlin有了更加完善的支持,我当时想使用同一份代码既兼容Java又兼容Kotlin,最后历经千辛万苦终于将Kotlin代码都调试通过了,却疏漏了Java部分的调试工作,我想当然地认为Java工程是没问题的,结果问题就出在Java工程上,会直接导致项目崩溃。这里我要再次感谢这位朋友帮我及时发现了这个Bug,不然就这么推送的话要造成非常严重的事故了。

但是在LitePal接口设计上关于Java和Kotlin共存的问题,昨天我Google了半天也没能找到任何完美的解决方案,于是我只好换了一种思路,不再强求一份代码同时兼容Java和Kotlin了,而是对LitePal的架构重新进行了划分,将LitePal整个工程分成了三个模块,Core、Java和Kotlin。Core模块中是所有LitePal核心功能的代码,Java模块中则是Java对外接口的这部分代码,Kotlin模块中是Kotlin对外接口的这部分代码。所以如果你是使用Java语言开发Android项目,那么就集成Core和Java这两个库,如果你是使用Kotlin语言的话,那就集成Core和Kotlin这两个库就行了。

由于这次改动涉及到整个LitePal架构的重新划分,还要向jcenter提交两个新的仓库,审核也需要时间,因此今天的这篇文章实在是发不出来了,不过我一定会尽力在本周之内解决所有的问题并且发布一个稳定的LitePal 2.1.0版本,希望大家能理解。

今天为大家选取了另外一篇优质文章,我们仍然干货不断。


作者简介


本篇转载自 刘强东 的博客文章,讲解了关于 Android中Button控件的详细知识,一起来看看!希望大家喜欢。

刘强东 的博客地址:

https://juejin.im/user/57c9a80379bc440063f3e0dc

本文原文地址:

https://juejin.im/post/5b0e8778f265da08d057b3db


前言


选择按钮在开发过程中也是使用频率非常高的控件. 但是我发现很多人都是一知半解, 总结下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作为示例

640?wx_fmt=other

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="设计师吴彦祖"
        />

效果图:

640?wx_fmt=other

RadioButton

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

使用起来很简单:

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

640?wx_fmt=other

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>

640?wx_fmt=gif

可以看到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

640?wx_fmt=other 

640?wx_fmt=gif

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. 但是拥有平滑切花动画. 支持自定义样式.

640?wx_fmt=other

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

api v21 才能使用的属性

android:showText // 在按钮上面显示文本内容: (开启/关闭)
android:splitTrack // 轨道和按钮分开(即不会重叠在一起)

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

文本内容自定义

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

640?wx_fmt=gif

自定义Track的高度和宽度都受到Thumb的限制.代码的功能XML属性全部都实现。


欢迎长按下图 -> 识别图中二维码

或者 扫一扫 关注我的公众号

640.png?

640?wx_fmt=jpeg

猜你喜欢

转载自blog.csdn.net/c10WTiybQ1Ye3/article/details/82977648