• Android Button点击效果(按钮背景变色、文字变色)


    一、 说明

    Android Button的使用过程中,我们会需要为Button添加点击效果,不仅仅按钮的背景色需要变化,而且有时,我们连文字的颜色都希望变化,我们可以使用StateListDrawable资源可以实现。


    二、 实现按钮点击的变化

    2.1 实现效果:



    2.2 我们首先需要定义按钮的背景的资源文件,我们使用图片资源来实现点击变化

    selector_btn_click_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/load_cofirm_btn_normal" android:state_enabled="true" android:state_window_focused="false"/>
        <item android:drawable="@drawable/load_cofirm_btn_normal" android:state_enabled="false"/>
        <item android:drawable="@drawable/load_cofirm_btn_press" android:state_pressed="true"/>
        <item android:drawable="@drawable/load_cofirm_btn_normal" android:state_focused="true"/>
    
    </selector>


    2.2 关于按钮的文字变化,我们使用颜色资源,定义color.xml,注意,在里面加入以下自定义颜色(注意不是用color标签)的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <drawable name="red">#f00</drawable>
        <drawable name="gray">#ccc</drawable>
    </resources>

    2.3 在res下新建drawable目录,里面新建selector_btn_click_bg.xml和selector_btn_click_text_color.xml文件,分别来设置按钮背景色和文字颜色

    selector_btn_click_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/load_cofirm_btn_normal" android:state_pressed="false"/>
        <item android:drawable="@drawable/load_cofirm_btn_normal" android:state_focused="false"/>
        <item android:drawable="@drawable/load_cofirm_btn_press" android:state_pressed="true"/>
        <item android:drawable="@drawable/load_cofirm_btn_press" android:state_focused="true"/>
    </selector>
    

    selector_btn_click_text_color.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="false" android:color="@drawable/red"/>
        <item android:state_focused="true" android:color="@drawable/gray"/>
        <item android:state_pressed="false" android:color="@drawable/red"/>
        <item android:state_press="true" android:color="@drawable/gray"/>
    </selector>

    2.4 在布局的按钮上,添家statelistdrawable修饰

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_btn_click_bg"
            android:text="点击我啊,看我变一变"
            android:textColor="@drawable/selector_btn_click_text_color" />


    运行程序,可看到我们想要的效果。


    程序代码(免费):http://download.csdn.net/detail/zuiwuyuan/7975847


  • 相关阅读:
    C# 使用EWS 读取 Exchange 未读邮件并设置为已读
    .NET 操作 Exchange示例
    Elsa-Core 工作流 使用代码启动审批流或触发审批任务
    Elsa-Core-workflow 从代码启动指定工作流
    .NET CORE 开源工作流 elsa-core 基础使用示例
    文档翻译经验分享(Markdown)
    Orchard Core 开发示例教程
    VMware 与 Hyper-v 并存, 可同时运行 非启动项
    SmtpException: Failure sending mail. ---> System.InvalidOperationException: Asynchronous operations are not allowed in this context.
    Linux (Deppin ,Ubuntu )开发环境配置,VUE & dotnetcore 解决 yarn 找不到问题
  • 原文地址:https://www.cnblogs.com/hehe520/p/6330018.html
Copyright © 2020-2023  润新知