美文网首页Android
android button 的点击事件

android button 的点击事件

作者: guoheng1996 | 来源:发表于2018-12-06 17:15 被阅读0次

android 的button 应该是几乎每天都用到的控件了吧,但是他的点击效果很难控制,很烦。。。

先来看看官方的默认button

    <android.support.v7.widget.AppCompatButton
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:layout_marginTop="150dp" />
简书不能上传视频,只能传GIF格式的,有点烦。。 nomal_button.2018-12-06 14_52_07.gif

感觉挺好看的,有阴影,有波纹,好像还有点点击时颜色加深
但是我们一般的话都是得加个背景色,或者加个边框啥的。。

   android:background="@color/black_4A4A4A"
black_button.2018-12-06 14_50_52.gif

设置这个属性之后,就只有阴影了,没有那个波纹效果了,有点难受

还有一个属性backgroundtint 这个属性只有在21以上才能用,
看看效果:

   android:backgroundTint="@color/black_4A4A4A"
black_tint.2018-12-06 15_07_06.gif

这个有阴影和波纹 看起来挺好

那么backroundTint是个啥东西呢?
请看这个:https://blog.csdn.net/huangxiaoguo1/article/details/63282956
感兴趣的话可以去看看。

但是这个tint只能设置颜色,如果我们想做成一个圆角的button,这个方法就没用了。

是不是没办法了呢?

将就一下:
或者我们可以自己写一个波纹效果ripple。这个也是21以上才能用的。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/xxxxx">
    <item android:drawable="@drawable/xxxx" />
</ripple>

android:color 这个颜色就是波纹的颜色,

android:drawable 这个就是你的button的样子。 ripple.2018-12-06 15_35_42.gif

默认是有阴影的,不过我去掉了。。

自己写的好像不是那个好看。。

哪为了兼容4.4,只能再将就一下,点击时不要ripple了,设置两个drawable,点击的时候切换。

还有没有其他办法呢?

曾经真的以为button的点击效果就这样了,
难看的自定义波纹也只能将就了,
直到我遇到materialbutton,
让我见一次,就久久不能忘记她哪支持各种式样的波纹效果.

先来看看官方给的demo。

material.2018-12-06 16_08_06.gif material.2018-12-06 16_10_16.gif

这样看起来没啥,主要是他的属性:

https://developer.android.com/reference/com/google/android/material/button/MaterialButton

Add icons to the start or center of this button of this button using the [app:icon], [app:iconPadding],[app:iconTint],[app:iconTintMode]and[app:iconGravity]

Specify background tint using the [app:backgroundTint]= and[app:backgroundTintMode]` attributes, which accepts either a color or a color state list.

Ripple color / press state color can be specified using the `[app:rippleColor] attribute. Ripple opacity will be determined by the Android framework when available. Otherwise, this color will be overlaid on the button at a 50% opacity when button is pressed.

Set the stroke color using the [app:strokeColor] attribute, which accepts either a color or a color state list. Stroke width can be set using the[app:strokeWidth] attribute.

Specify the radius of all four corners of the button using the [app:cornerRadius] attribute.

方法名 作用
app:icon 为button添加一个icon
app:backgroundTint和backgroundTintMode 为button添加一个背景色
app:rippleColor 设置波纹颜色
app:cornerRadius 为button设置圆角
app:strokeWidth 设置边框宽度
app:strokeColor 设置边框颜色
app:icon 为button添加一个icon

直接使用属性设置一个背景色和边框,岂不是美滋滋。。。

那就来试试吧:

官方提供来一下几种style:

    <style name="Widget.MaterialComponents.Button.Icon">
    <style name="Widget.MaterialComponents.Button.OutlinedButton">
    <style name="Widget.MaterialComponents.Button.OutlinedButton.Icon">
    <style name="Widget.MaterialComponents.Button.TextButton">
    <style name="Widget.MaterialComponents.Button.UnelevatedButton">
    <android.support.design.button.MaterialButton
        android:layout_width="150dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_height="150dp"
        android:layout_gravity="center"/>
material_outlined.2018-12-06 17_03_30.gif

看起来挺好,但是当我改变背景色的时候,问题来了。

device-2018-12-06-164539.png

仔细看边框的4个角,她处理的好像不太好,

继续变大角度

    <android.support.design.button.MaterialButton
        android:layout_width="150dp"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_height="150dp"
        app:backgroundTint="@color/red_dd2d34"
        app:strokeColor="@color/black_000000"
        app:strokeWidth="5dp"
        app:cornerRadius="5dp"
        android:layout_gravity="center"/>
device-2018-12-06-170756.png

为啥官方的好像没事呢?

点进去style看看就好了

  <style name="Widget.MaterialComponents.Button.OutlinedButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="android:paddingLeft">@dimen/mtrl_btn_padding_left</item>
    <item name="android:paddingRight">@dimen/mtrl_btn_padding_right</item>
    <item name="strokeColor">@color/mtrl_btn_stroke_color_selector</item>
    <item name="strokeWidth">@dimen/mtrl_btn_stroke_size</item>
  </style>
  <style name="Widget.MaterialComponents.Button.TextButton" parent="Widget.MaterialComponents.Button.UnelevatedButton">
    <item name="android:textColor">@color/mtrl_text_btn_text_color_selector</item>
    <item name="android:paddingLeft">@dimen/mtrl_btn_text_btn_padding_left</item>
    <item name="android:paddingRight">@dimen/mtrl_btn_text_btn_padding_right</item>
    <item name="iconTint">@color/mtrl_text_btn_text_color_selector</item>
    <item name="iconPadding">@dimen/mtrl_btn_text_btn_icon_padding</item>
    <item name="backgroundTint">@color/mtrl_btn_transparent_bg_color</item>
    <item name="rippleColor">@color/mtrl_btn_text_btn_ripple_color</item>
  </style>
    <color name="mtrl_btn_transparent_bg_color">#00ffffff</color>

默认是把button的背景设为全透明,于是我给官方demo添加一个背景色后变成这样了:

demo_black.png

哇,,,,想哭,,怎么会这样呢?

回去将就的自己写ripple吧!

相关文章

  • android button 的点击事件

    android 的button 应该是几乎每天都用到的控件了吧,但是他的点击效果很难控制,很烦。。。 先来看看官方...

  • Compose Button

    Button Compose 中的Button,对标Button ,是一个具有点击事件的一个控件,跟Android...

  • UIButton 事件监控和连点

    1、button点击事件监测 通过runtime获取button的点击事件,进行全局监测 2、button多次点击...

  • Android 之 Button (按钮)

    按钮: 效果: button点击事件: 第一种: 给xml中给button增加了android:onClick="...

  • 防止按钮重复点击

    防止按钮重复点击 按钮的 OnClick 事件是 Android 开发中最常见的事件,比如 Button 常常和 ...

  • 鸿蒙OS,Button的点击事件

    鸿蒙OS,Button的点击事件处理如下: 具体的点击事件代码:button.setClickedListener...

  • 禁止普通标签的点击事件

    禁止button的点击事件是disabled='true'禁止非button标签的点击事件是添加样式pointer...

  • Android_Button按键点击事件

    一.Button的基本属性: Button继承了TextView,所以TextView有的属性,Button具有T...

  • Button点击事件

    Button控件第一种点击事件 利用Button控件的onClick属性创建两个按钮btn_one与btn_two...

  • button点击事件

    在android开发中,监听button的点击事件有四种方法。 1:使用匿名内部类: 2:自定义单击事件监听类(继...

网友评论

    本文标题:android button 的点击事件

    本文链接:https://www.haomeiwen.com/subject/hayycqtx.html