首页 > 解决方案 > 有没有办法让 Android Studio 中的禁用按钮可点击?

问题描述

在 Android Studio 中,我有一个带有发送 SMS 消息的按钮的 Kotlin 项目。当应用程序的发送短信权限被拒绝时,该按钮被禁用。当用户单击禁用按钮时,我想向用户显示一条敬酒消息:

Toast.makeText(this, R.string.smsDisabled, Toast.LENGTH_LONG).show()

但是,经过大量研究,我无法使这个禁用的按钮可点击。我试过smsButton.isClickable = true按钮之类的东西,但它仍然不起作用。如果可以将按钮样式设置为简单地看起来像是被禁用,那也可以。我的按钮样式当前设置为@style/Widget.MaterialComponents.Button,我在更改按钮颜色方面没有任何成功。我尝试过应用背景、backgroundTint、自定义主题,并像在这个问题中那样创建自定义背景。

我是 Android Studio 的新手,所以我的知识和经验有限。有没有办法实现这个功能?

标签: androidandroid-studiokotlinandroid-layout

解决方案


这是您可以执行的操作:

if (isPermissions) {
        //keep the button as it is or do what you have to when button is enabled
    } else {
        //change the button color and maybe icon two
        button.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.grey))
        button.setOnClickListner {
            //show toast
            Toast.makeText(this, R.string.smsDisabled, Toast.LENGTH_LONG).show()
        }
    }

推荐阅读