有没有办法让 Android Studio 中的禁用按钮可点击?
Is there a way to make a disabled button clickable in Android Studio?
在 Android Studio 中,我有一个 Kotlin 项目,其中包含一个发送 SMS 消息的按钮。当应用程序的发送短信权限被拒绝时,该按钮将被禁用。当用户单击禁用按钮时,我想向用户显示一条消息:
Toast.makeText(this, R.string.smsDisabled, Toast.LENGTH_LONG).show()
但是,经过大量研究后,我无法使这个禁用的按钮可点击。我在按钮上尝试了 smsButton.isClickable = true
之类的东西,但它仍然不起作用。如果可以将按钮样式设置为简单地 看起来 就像它被禁用一样,那也可以。我的按钮样式当前设置为 @style/Widget.MaterialComponents.Button
并且我没有成功更改按钮颜色。我已尝试应用背景、backgroundTint、自定义主题,并创建自定义背景,如 this question.
我是 Android Studio 的新手,所以我的知识和经验有限。有没有办法实现这个功能?
isClickable
仅在使用相同属性禁用点击时才使其再次可点击。
尝试:
button.isEnabled = true
您可以执行以下操作:
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()
}
}
在 Android Studio 中,我有一个 Kotlin 项目,其中包含一个发送 SMS 消息的按钮。当应用程序的发送短信权限被拒绝时,该按钮将被禁用。当用户单击禁用按钮时,我想向用户显示一条消息:
Toast.makeText(this, R.string.smsDisabled, Toast.LENGTH_LONG).show()
但是,经过大量研究后,我无法使这个禁用的按钮可点击。我在按钮上尝试了 smsButton.isClickable = true
之类的东西,但它仍然不起作用。如果可以将按钮样式设置为简单地 看起来 就像它被禁用一样,那也可以。我的按钮样式当前设置为 @style/Widget.MaterialComponents.Button
并且我没有成功更改按钮颜色。我已尝试应用背景、backgroundTint、自定义主题,并创建自定义背景,如 this question.
我是 Android Studio 的新手,所以我的知识和经验有限。有没有办法实现这个功能?
isClickable
仅在使用相同属性禁用点击时才使其再次可点击。
尝试:
button.isEnabled = true
您可以执行以下操作:
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()
}
}