关于android:focusable和focusableInTouchMode之间的区别?

Difference between focusable and focusableInTouchMode?

我想知道它们之间的实际区别...何时应分别使用,应如何使用以及在哪些情况下有帮助?

举一些例子并详细解释。


在Android开发者博客中对此进行了说明:http://android-developers.blogspot.co.at/2008/12/touch-mode.html

以下引号应使其清楚:

By itself, the touch mode is something very easy to understand as it simply indicates whether the last user interaction was performed with the touch screen. For example, if you are using a G1 phone, selecting a widget with the trackball will take you out of touch mode;

...

In touch mode, there is no focus and no selection. Any selected item
in a list of in a grid becomes unselected as soon as the user enters
touch mode. Similarly, any focused widgets become unfocused when the
user enters touch mode.

...

Now that you know focus doesn't exist in touch mode, I must explain that it's not entirely true. Focus can exist in touch mode but in a very special way we call focusable in touch mode. This special mode was created for widgets that receive text input, like EditText or, when filtering is enabled, ListView.

...

Focusable in touch mode is a property that you can set yourself either
from code or XML. However, it should be used sparingly and only in
very specific situations as it breaks consistency with Android normal
behavior. A game is a good example of an application that can make
good use of the focusable in touch mode property. MapView, if used in
fullscreen as in Google Maps, is another good example of where you can
use focusable in touch mode correctly.


Give some example and explain them in detail

我会给你我自己的经验:

我有一个Google TV应用程序,该应用程序具有大量的ImageButtons。

我希望ImageButtons是可选的。

因此,如果有人用鼠标或遥控器单击它们,则只会被选中(在我的情况下为突出显示)。然后,如果用户按下所选的ImageButton,则触发该动作。通过在XML布局中启用focusableInTouchMode属性,可以实现此确切行为。

我要做的就是为ImageButton和voila设置一个普通的onClickListener

我还没有在手机上检查过我的应用程序,但是我想它将带来熟悉的结果。

编辑

When?

我已经告诉您一个我测试过的用例:当您希望Button的onClickListener在您第一次单击并选择Button之后,在第二次单击时触发操作。

我使用第一次单击来获得"焦点",并在按钮上显示"放大比例放大"动画。

How?

只需在XML布局文件中将按钮的属性focusableInTouchMode设置为true


聚焦是一种查看状态,通常可以使用轨迹球和dpad更改焦点。重点关注状态时,您的视图可以具有不同的背景。

在触摸模式下可聚焦,当用户触摸视图时可以使视图获得焦点,这种组件的一个很好的例子是EditText

使用Button或任何可单击的组件处于按下状态通常是您感兴趣的。


用户可以通过使用硬件键或按钮或触摸屏幕来与其设备进行交互。触摸屏幕可使设备进入触摸模式。然后,用户可以通过触摸屏幕上的虚拟按钮,图像等与之交互。

要检查设备是否处于触摸模式,请调用View类的isInTouchMode()方法。