Android focusable 和 focusableInTouchMode 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24155956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:50:40  来源:igfitidea点击:

Difference between focusable and focusableInTouchMode?

androidfocusfocusable

提问by Jawad Zeb

I want to know the actual difference between them... When should each be used, how should each be used, and in which situations is each helpful?

我想知道它们之间的实际区别......应该在什么时候使用,应该如何使用,在什么情况下有用?

Give some examples and explain them in detail.

举一些例子并详细解释它们。

采纳答案by Apfelsaft

It is explained in the Android Developers Blog: http://android-developers.blogspot.co.at/2008/12/touch-mode.html

它在 Android 开发者博客中有解释:http: //android-developers.blogspot.co.at/2008/12/touch-mode.html

The following quotes should make it clear:

以下引述应该清楚说明:

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;

就其本身而言,触摸模式很容易理解,因为它只是表明上次用户交互是否是通过触摸屏执行的。例如,如果您使用的是 G1 手机,选择带有轨迹球的小部件将使您脱离触摸模式;

...

...

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.

既然您知道在触摸模式下不存在焦点,我必须解释这并不完全正确。焦点可以存在于触摸模式中,但以一种非常特殊的方式,我们在触摸模式中称为可聚焦。这种特殊模式是为接收文本输入的小部件创建的,如 EditText 或启用过滤时的 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.

在触摸模式下可聚焦是一种属性,您可以通过代码或 XML 自行设置。但是,它应该谨慎使用,并且仅在非常特定的情况下使用,因为它破坏了与 Android 正常行为的一致性。游戏是一个很好的应用程序示例,它可以很好地利用可聚焦的触摸模式属性。MapView,如果在谷歌地图中全屏使用,是另一个很好的例子,你可以在触摸模式下正确使用focusable。

回答by Behnam

Give some example and explain them in detail

举一些例子并详细解释它们

I'll give you my own experience:

我给你讲一下我自己的经验:

I had a Google TV application which had an activity with a great deal of ImageButtons.

我有一个 Google TV 应用程序,它有一个包含大量 ImageButton 的活动。

I wanted the ImageButtons to be selectable.

我希望 ImageButtons 是可选择的。

So if a person clicks on them with mouse or remote controller, they become selected only (Highlighted in my case). Then if the user presses the selected ImageButton, the action triggers. This exact behaviour was achieved through enabling the focusableInTouchModeproperty through the XML layout.

所以如果一个人用鼠标或遥控器点击它们,它们只会被选中(在我的例子中突出显示)。然后,如果用户按下选定的ImageButton,则触发该操作。这个确切的行为是通过通过focusableInTouchModeXML 布局启用属性来实现的。

All I had to do was to set an ordinary onClickListenerfor the ImageButtons and voila!

我所要做的就是onClickListener为 ImageButtons设置一个普通的,瞧!

I haven't checked my application on handset but I guess it would deliver familiar result.

我还没有在手机上检查过我的应用程序,但我想它会提供熟悉的结果。

EDIT

编辑

When?

什么时候?

I've told you a use case I have tested: When you want your Button's onClickListenerto trigger action on your second click, after you have first clicked and selected the Button.

我告诉过你一个我测试过的用例:当你希望你的 ButtononClickListener在你第二次点击时触发动作,在你第一次点击并选择Button.

I Used the first click to gain "focus" and display a Zoom-In scale Up animation on my button.

我使用第一次点击获得“焦点”并在我的按钮上显示放大动画。

How?

如何?

Just set the button's property focusableInTouchModeto truein your XML layout file.

按钮的属性刚刚成立focusableInTouchModetrue你的XML布局文件。

回答by Niko

Focused is a state for view and generally focus can be changed with trackball and dpad. Your view can have different background when state is focused.

Focused 是一种视图状态,通常可以通过轨迹球和 dpad 更改焦点。当状态为焦点时,您的视图可以有不同的背景。

Focusable in touch mode allows view to gain focus when user is touching the view, good example of this kind of component is EditText.

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

With Buttonor any clickable component pressed state is usually what you are interested in.

带有Button或任何可点击组件的按下状态通常是您感兴趣的。

回答by M_ Fa

Users can interact with their devices by using hardware keys or buttons, or by touching the screen. Touching the screen puts the device into touch mode. The user can then interact with it by touching the on-screen virtual buttons, images, etc.

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

To check if the device is in touch mode, call the View class's isInTouchMode()method.

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