SetEnable()与 setClickable() ,有什么区别?

直到现在,当我想阻止用户按下按钮时,我会设置 button.setClickable(false);,通常会将文本改为某种灰色(让用户知道按钮已被禁用)。今天我偶然发现了 setEnabled()的属性。

因此,我查看了下面的文档,看到了该方法的解释:

setEnabled(boolean enabled)
Set the enabled state of this view.

这到底是什么意思?启用状态/可点击状态和禁用状态/不可点击状态的区别是什么?有没有人可以解释一下,使用可点击属性和使用 setEnabled()属性之间的区别?什么时候应该使用什么?我搜索了 Stack Overflow,但没有找到任何相关内容。

80472 次浏览

What the hell is that mean?

Quoting the Wikipedia page for "GUI widget":

In the context of an application, a widget may be enabled or disabled at a given point in time. An enabled widget has the capacity to respond to events, such as keystrokes or mouse actions. A widget that cannot respond to such events is considered disabled. The appearance of disabled widget is typically different from an enabled widget; the disabled widget may be drawn in a lighter color, or may be visually obscured in some way. See the image to the right for an example.

This concept has been around for a couple of decades and can be found in most GUI frameworks.

what is the difference between enable state/clickable state and disabled state/ not clickable state?

In Android, a widget that is not clickable will not respond to click events. A disabled widget not only is not clickable, but it also visually indicates that it is disabled.

what do you mean by: "..since it makes the Button visually "disabled"? how does it changes it visually?

What makes a Button look and respond like a Button is its background, which is a StateListDrawable. There is a specific image used for the disabled state.

So basically an enabled false doesn't respond to any response and an clickable false still response when set at runtime and trust me I just tried it.

As Dilip said, setClickable does not works if set at runtime. Here is a trick to make it working:

ToggleButton toggle = ...
toggle.setOnTouchListener(new ToggleButton.OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
// If true is returned, the event is eated by the TouchListener
return !v.isClickable();
}
});

Views can also respond to external keyboards, directional pads (remote/gaming controllers), and assistive devices (switch, screen readers).

The differences is listed above, but there is a tip. use setClickable() after setOnClickListener(). Because of this:

public void setOnClickListener(@Nullable OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
getListenerInfo().mOnClickListener = l;
}

setClickable public void setClickable (boolean clickable)

It enables or disables click events for the particular view. When a view is clickable it will change its state to "pressed" on every click. if this property of view is disabled then it will not change its state.

setEnabled public void setEnabled (boolean enabled)

It set the enabled state of this view .If the particular view is set to be enabled then pass true in the parameter else pass false

A big difference I don't see mentioned elsewhere is with overlapping Views. A View with clickable=true and enabled=false won't allow you to press a View behind it. But a View with clickable=false will allow you to press a View behind it.