惊落一身雪 发表于 2025-3-15 19:19:21

Android UI 组件系列(二):Button 进阶用法

引言

在上一篇博客中,我们先容了 Button 的基本用法和常见属性,掌握了 Button 的底子知识。然而,在实际开发中,Button 远不止于简朴的点击功能,它还可以支持不同的变体、丰富的自定义样式,以及更灵活的状态管理。
本篇博客将深入探讨 Button 的进阶用法,包罗事件处置惩罚、不同范例的 Button 变体、自定义样式,以及怎样使用 StateListDrawable 来管理 Button 的不同状态。此外还会先容一些更高级的用法,如Jerpack Compose 中的 Button 处置惩罚方式,资助各人在不同的场景下灵活使用 Button。
Button的事件处置惩罚

Button 最重要的作用就是响应用户的点击操作。无论是提交表单、跳转页面、还是实行某个功能,阿牛的交互本事都是至关紧张的。
在实际开发中,Button 提供了多种事件监听方式,例如单击(setOnClickListener)、长按(setOnLongClickListener)、触摸监听(setOnTouchListener)等,除此之外,我们还可以控制按钮的点击状态,让它在特定的环境下启用或禁用。
接下来我们就来具体相识 Button 的事件处置惩罚方式,并看看怎样在项目中正确使用它们。
单击事件(setOnClickListener)

用户轻点按钮时触发,是按钮最常用的事件。
/// 设置按钮
    private void setupButton() {
      Button button = findViewById(R.id.button);
      // 单击事件
      button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "按钮被点击了!", Toast.LENGTH_SHORT).show();
            }
      });
    } 长按事件(setOnLongClickListener)

用户按住按钮不放,会触发长按事件,适用于显示弹窗、删除确认等操作。
      // 长按事件
      button.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(MainActivity.this, "按钮被长按了!", Toast.LENGTH_SHORT).show();
                return true;// 返回 true 表示事件被消费,不会触发 onClick 事件
            }
      });
当返回true时,事件被消费,不会再出发onClick事件,当返回false时,长按事件触发,松手后,仍然会触发onClick事件。
触摸事件监听(setOnTouchListener)

可以监听手指的按下、移动、松开等操作,适用于实现拖拽等高级交互。
button.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Toast.makeText(MainActivity.this, "手指按下", Toast.LENGTH_SHORT).show();
                break;
            case MotionEvent.ACTION_MOVE:
                Toast.makeText(MainActivity.this, "手指移动", Toast.LENGTH_SHORT).show();
                break;
            case MotionEvent.ACTION_UP:
                Toast.makeText(MainActivity.this, "手指松开", Toast.LENGTH_SHORT).show();
                break;
      }
      return true;// 返回 true,拦截事件,不触发 onClick
    }
});
触摸事件常见的MotionEvent范例:


[*]ACTION_DOWN:手指按下时触发。
[*]ACTION_MOVE:手指移动时触发。
[*]ACTION_UP:手指抬起时触发。
启用/禁用按钮交互(setEnabled(false))

// 禁用按钮(变灰且不可点击)
button.setEnabled(false);

// 启用按钮(恢复可点击)
button.setEnabled(true);
通常会搭配StateListDrawable改变按钮颜色。
Button 自定义样式

Android 的 Button 默认样式可能无法满意UI设计需求,我们可以通过 修改背景、圆角、阴影、渐变、字体 等方式来自定义 Button,让它更加雅观和符合应用的设计风格。
自定义圆角按钮

默认的 Button 圆角可能并不能符合设计要求,我们可以使用shape定义一个圆角背景。
   res/drawable/btn_round.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20dp"/>
    <solid android:color="@color/blue"/>
</shape>
效果如下:
https://i-blog.csdnimg.cn/direct/c3d4ea89b1eb4fde9f34b1a1b261c1e5.png
 
添加点击时的不同状态(StateListDrawable)

让按钮在按下、禁用、默认状态下来显示不同的样式。(注意设置app:backgroundTint="@null")
   res/drawable/button_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按下状态 -->
    <item android:state_pressed="true" android:drawable="@color/teal_200"/>
    <!-- 禁用状态 -->
    <item android:state_enabled="false" android:drawable="@color/purple_700"/>
    <!-- 默认状态 -->
    <item android:drawable="@drawable/btn_round"/>
</selector> 效果如下:
默认状态
https://i-blog.csdnimg.cn/direct/c3d4ea89b1eb4fde9f34b1a1b261c1e5.png
按下状态
https://i-blog.csdnimg.cn/direct/1d2a1a4f297d4fe4b0016c555a1cc1ff.png
 

按钮支持渐变色

使用自定义shape,让按钮具有渐变色,使其更有质感。
   res/drawable/btn_gradient.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient
      android:startColor="@color/purple_200"
      android:endColor="@color/purple_500"
      android:angle="45"/>
    <corners android:radius="20dp"/>
</shape> 在Button中使用
   <Button
      android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="圆角按钮"
      android:background="@drawable/btn_gradient"
      app:backgroundTint="@null"/>
效果如下:
https://i-blog.csdnimg.cn/direct/e585115e290d430aa5eef21c8b621ad9.png
 

使用StateListDrawable 管理按钮状态

在设置按钮不同状态的时候其实我们已经使用到了StateListDrawable。在 Android 开发中,按钮的交互体验至关紧张。例如,我们渴望按钮在 按下、禁用、获得焦点 等不同状态下显示不同的效果,而不是一直保持相同的外貌。
如果你在代码中手动监听setOnTouchListener 来改变背景颜色,这不仅贫困,而且难以维护。StateListDrawable 允许我们通过XML直接定义不同状态下的按钮外貌,大大简化了状态管理,接下来,我们来看 StateListDrawable 怎样实现按钮的动态变化。
常见的按钮状态:

[*]android:state_pressed="true":按下(Pressed)。
[*]android:state_selected="true":选中(Selected)。
[*]android:state_focused="true":焦点(Focused)。
[*]android:state_enabled="true":启用(Enabled)。
[*]android:state_enabled="false":禁用(Disabled)。
使用 StateListDrawable 设置不同状态的颜色

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按下状态 -->
    <item android:state_pressed="true" android:drawable="@color/teal_200"/>
    <!-- 禁用状态 -->
    <item android:state_enabled="false" android:drawable="@color/purple_700"/>
    <!-- 默认状态 -->
    <item android:drawable="@color/blue"/>
</selector> 在Button中使用
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="按钮"
      android:background="@drawable/button_selector"
      app:backgroundTint="@null" />
 
使用 StateListDrawable 结合 shape实现圆角按钮

如果你渴望按钮在不同状态下不仅颜色不同,还带有 圆角或阴影,你可以将drawable由颜色更换为shape。
创建button_default.xml 默认样式
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/blue"/>
    <corners android:radius="20dp"/>
</shape>
创建 button_pressed.xml 按下样式
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/green"/>
    <corners android:radius="20dp"/>
</shape>
创建 button_disabled.xml 禁用样式
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/gray"/>
    <corners android:radius="20dp"/>
</shape>
创建button_selector.xml 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
    <item android:state_enabled="false" android:drawable="@drawable/button_disabled"/>
    <item android:drawable="@drawable/button_default"/>
</selector>
使用
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="按钮"
      android:background="@drawable/button_selector"
      app:backgroundTint="@null" />
注意:记得在color内声明颜色嗷!
使用 StateListDrawable 结合 TextColor 实现按钮笔墨颜色变化。
创建text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/white"/>
    <item android:state_enabled="false" android:color="@color/gray"/>
    <item android:color="@color/black"/>
</selector>
在Button 中使用
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="按钮"
      android:background="@drawable/button_selector"
      android:textColor="@drawable/text_selector"
      app:backgroundTint="@null" />
结语

在这篇博客中,我们深入探讨了 Button 的进阶用法,重点先容了怎样通过 事件处置惩罚 来响应用户的操作,怎样使用 自定义样式 来提升按钮的外貌,并使用 StateListDrawable 来管理按钮在不同状态下的表现。这些本事能够资助你打造更具交互性、视觉吸引力和动态响应的按钮,提升用户体验。
无论是通过 Java 代码处置惩罚事件,还是通过 XML 实现样式的切换,这些方法都能让你的应用界面更加生动和灵活。掌握这些本事后,你可以更轻松地定制按钮的行为,打造符合需求的 UI 组件。
渴望这篇博客能够资助你在日常开发中更好地使用和自定义按钮,创造出更加出色的 Android 应用!如有疑问或想法,欢迎在评论区分享。
 
 
 
 
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Android UI 组件系列(二):Button 进阶用法