setEnabled() vs setClickable(), what is the difference?

From: http://stackoverflow.com/questions/15615823/setenabled-vs-setclickable-what-is-the-difference

setEnabled() vs setClickable(), what is the difference?

Until now, when I wanted to block the user from pressing the button, I would set thebutton.setClickable(false);, and usually change the text to some kind of gray (to let the user know that the button is disabled). today I stumble on the setEnabled() property.

So I went to the documentation to see that:

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

What the hell is that mean? what is the difference between enable state/clickable state and disabled state/ not clickable state? Some one could please explain what is the difference between doing what I was doing until now (using the clickable property) and using the setEnable property? what should be used when? I search SO but could not find something related.

Thanks.

share | improve this question
 
3 
I would guess that setEnabled (false) makes a View non-clickable AND non-focusable which means it gets completely locked. Also, setEnabled(false) is a preferred way in case of a Button since it makes the Button visually "disabled" –  Droidman Mar 25 '13 at 13:24
 
what do you mean by: "..since it makes the Button visually "disabled"? how does it changes it visually? and what if I use a custom selector? –  Emil Adz Mar 25 '13 at 13:29
 
not sure about a custom selector, have not tested it. But in case of a standard Button, it gets semi-transparent so you don't need to do it by hand –  Droidman Mar 25 '13 at 13:32
up vote 16 down vote accepted

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.

share | improve this answer
 
 
nice one. Though I was pretty much sure that only the alpha value of Button's background gets changed and there's no specific drawable –  Droidman Mar 25 '13 at 13:49
 
@Maver1ck: The Button StateListDrawable definitely has a disabled state: github.com/android/platform_frameworks_base/blob/master/core/… It's possible that there are other effects applied by the Button class, though I am not aware of any. –  CommonsWare Mar 25 '13 at 13:53
 
I just made a small test and it seems like for Buttons, the text color is changed as well. It gets gray when I set the button's enabled to false. –  FD_ Mar 25 '13 at 13:54
 
so basically if I use a custom selector? I should specify a certain drawable if I would want to use the setEnable() method on this specific button (and to get a feedback on this change)? –  Emil Adz Mar 25 '13 at 13:54
2 
@EmilAdz: If you change the text color, or you change the button background, you will want to have an appropriate entry in the ColorStateList or StateListDrawable for a disabled state. That entry will be used when setEnabled(false) is called, or android:enabled="false" is used in a layout. –  CommonsWare Mar 25 '13 at 13:57

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值