Android中Button.setClickable(false)不起作用

本文探讨了在程序中使用Button.setClickable(false)时遇到的问题及解决方法。问题在于此方法在setOnClickListener()方法之前调用时不起作用。正确的做法是将setClickable(false)放在setOnClickListener()之后。

我在程序中多次使用了Button.setClickable(false),但是首次初始化的时候都不起作用,怎么看程序都没有错误,然后查到了以下问题

在setOnClickListener()方法中有这样一段代码:

[java]  view plain copy
  1. if (!isClickable()) {  
  2.            setClickable(true);  
  3. }

而我初始化的时候这个方法卸载setOnClickListener()之前了。



解决办法:

setClickable(false)方法要且一定要在setOnClickListener()方法之后,才能得到正确的效果。


下面代码一直设置的clickable都是不可点击,这样相关点击事件如何触发 mBinding.newDeviceAlertsSw.setActionCheckedChangeListener((button, isCheck, fromUser) -> { mBinding.alertMethodTitle.setVisibility(isCheck ? View.VISIBLE : View.GONE); mBinding.connectionAlertsCard.setVisibility(isCheck ? View.VISIBLE : View.GONE); if (fromUser) { mViewModel.updateNewDeviceAlerts(isCheck, mBinding.pushNotificationItem.isActionChecked() ? AlertMethod.PUSH_NOTIFICATIONS : AlertMethod.EMAIL); mBinding.newDeviceAlertsSw.getActionSwitch().setStateLoading(true); mBinding.newDeviceAlertsSw.getActionSwitch().setClickable(false); } }); mBinding.pushNotificationItem.getActionRadio().setClickable(false); mBinding.pushNotificationItem.getContent().setSingleLine(false); mBinding.emailAlertsItem.getActionRadio().setClickable(false); mBinding.emailAlertsItem.getContent().setSingleLine(false); mBinding.pushNotificationItem.setOnClickListener(v -> { if (!mBinding.pushNotificationItem.isActionChecked()) { showNewDeviceAlertsView(true, true, false); mViewModel.updateNewDeviceAlerts(true, AlertMethod.PUSH_NOTIFICATIONS); mBinding.emailAlertsItem.getActionRadio().setStateLoading(false); mBinding.emailAlertsItem.getActionRadio().setClickable(false); mBinding.pushNotificationItem.getActionRadio().setStateLoading(true); mBinding.pushNotificationItem.getActionRadio().setClickable(false); } }); mBinding.emailAlertsItem.setOnClickListener(v -> { if (!mBinding.emailAlertsItem.isActionChecked()) { showNewDeviceAlertsView(true, false, true); mViewModel.updateNewDeviceAlerts(true, AlertMethod.EMAIL); mBinding.pushNotificationItem.getActionRadio().setStateLoading(false); mBinding.pushNotificationItem.getActionRadio().setClickable(false); mBinding.emailAlertsItem.getActionRadio().setStateLoading(true); mBinding.emailAlertsItem.getActionRadio().setClickable(false); } });
最新发布
11-04
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值