android - 有没有办法让 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 的新手,所以我的知识和经验有限。有没有办法实现这个功能?
解决方案
这是您可以执行的操作:
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()
}
}
推荐阅读
- javascript - 流程:无法获取 this.props.width,因为 T 中缺少属性宽度
- python-2.7 - ERAlchemy 无法连接到数据库
- android - 如果我的 Android 应用程序需要 NFC 权限但实际上没有使用它怎么办?
- python - Python 在使用 zip() 迭代列表时不会修改值,但在使用 enumerate() 时会这样做
- java - 使用 Vert.x 处理请求中间件中抛出的错误
- gcc - GCC 在哪里存储编译文件?
- java - 如何使用封面数组解析我的二维数组以查找字符?
- json - 尝试使用 Swift 在 Xcode 中使用 POST 请求将 json 格式的数据发送到本地 Web 服务器
- rest - Twilio 流程执行显示多个错误
- html - 打印时最后一页表行粘在页面底部