SoFunction
Updated on 2025-04-07

Android Development Notes: How to Block Button SetClickable and SetEnabled

Today I want to make the button invalid temporarily and can only be pressed by the user if it meets certain conditions. At first I thought it was setClickable, but I got it wrong.
Please see the manual:
Copy the codeThe code is as follows:

public void setClickable (boolean clickable)
Since: API Level 14
Sets whether this node is clickable.
Note: Cannot be called from an AccessibilityService. This class is made immutable before being delivered to an AccessibilityService.
Parameters
clickable True if the node is clickable.
Throws
IllegalStateException If called from an AccessibilityService.
public void setContentDescription (CharSequence contentDescr

This function means to press the key. For example, if you make a timer and simulate it on your phone, let the button press itself instead of manually pressing it. This function can be used.
Copy the codeThe code is as follows:

public void setEnabled (boolean enabled)
Since: API Level 14
Sets whether this node is enabled.
Note: Cannot be called from an AccessibilityService. This class is made immutable before being delivered to an AccessibilityService.
Parameters
enabled True if the node is enabled.
Throws
IllegalStateException If called from an AccessibilityService.

This function is the real thing, allowing a button to be pressed by the user or cannot be pressed. If set to false, the button will turn gray, and there will be no response when pressed. When set to true, it will be used normally. This is exactly the function I'm looking for.