禁用时更改FAB背景颜色 [英] Change FAB background color when disabled

查看:78
本文介绍了禁用时更改FAB背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在按下浮动按钮2秒钟后禁用浮动按钮的背景颜色.我还希望它在2秒的持续时间结束后恢复到原始颜色.

I'm trying to figure out how I can change the background color of the floating action button when it is disabled for a duration of 2 seconds after being pressed. I would also like it to return to its original color when the 2 second duration is over.

这是按下时2秒延迟的代码.这段代码在MainActivity的一个片段中.

This is the code for the 2 second delay when pressed. This code is in a fragment within the MainActivity.

 appBar.setExpanded(true, true);
 fab.setVisibility(View.VISIBLE);
 fab.setImageResource(R.drawable.ic_phone_white_18dp);

 fab.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
     fab.setClickable(false);
     Timer buttonTimer = new Timer();
     buttonTimer.schedule(new TimerTask() {
       @Override
       public void run() {
         runOnUiThread(new Runnable() {
           @Override
           public void run() {
             fab.setClickable(true);
           }
         });
       }
     }, 2000);

我尝试使用文档中的 StateListDrawable 方法,但是没有遇到任何可行的方法.

I've tried playing around with the StateListDrawable methods in the documentation but have not come across anything that works.

这是FAB颜色主题的XML

This is the XML for the color themes of the FAB

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/themeColorPressed" android:state_pressed="true"/>
    <item android:color="@color/themeColorPressed" android:state_checked="true"/>
    <item android:color="@color/themeColorPressed" android:state_selected="true"/>
    <item android:color="@color/themeColorPressed" android:state_enabled="false"/>
    <item android:color="@color/themeColor" android:state_enabled="true"/>
</selector>

推荐答案

禁用它时,只需调用 fab.setBackgroundColor(Color.GRAY); (或其他颜色)即可.您也可以使用 fab.setBackgroundColor(getResources().getColor(R.color.colorAccent0)); 使用资源颜色.

Just call fab.setBackgroundColor(Color.GRAY); (or whatever color) when you disable it. Also you can use fab.setBackgroundColor(getResources().getColor(R.color.colorAccent0)); to use a resource color.

这篇关于禁用时更改FAB背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆