Android setFocusable 和 setFocusableInTouchMode 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23799064/
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:28:49  来源:igfitidea点击:

What is the difference between setFocusable and setFocusableInTouchMode?

androidfocus

提问by Suragch

What is the difference between setFocusableand setFocusableInTouchModefor an Android view?

Android 视图setFocusablesetFocusableInTouchModeAndroid 视图有什么区别?

Additional questionafter trying to read the documentation:

尝试阅读文档后的其他问题

And how are these different from isFocusableand isFocusableInTouchMode?

这些与isFocusableisFocusableInTouchMode有何不同?

回答by Rajkumar

setFocusablemainly used for enable/disable view's focus event on both touch mode and keypad mode( using up/down/next key).

setFocusable主要用于在触摸模式和键盘模式(使用上/下/下一个键)下启用/禁用视图的焦点事件。

setFocusableInTouchModemainly used for enable/disable view's focus event on touch mode alone.

setFocusableInTouchMode主要用于单独在触摸模式下启用/禁用视图的焦点事件。

If you are disabled setFocusableit also disabled the view's focus event on touch mode.

如果您禁用了setFocusable,它还会在触摸模式下禁用视图的焦点事件。

回答by Suragch

After reading the answer from @Raj, going through the Android documentation, and experimenting with the code, I think I understand how they work a little better now. Here is a little additional help if anyone else is similarly confused. Let me know if I've got it wrong.

在阅读了@Raj 的答案,浏览了 Android文档并尝试了代码之后,我想我现在理解它们的工作原理了。如果其他人也有类似的困惑,这里有一些额外的帮助。如果我弄错了,请告诉我。

Views can be focused or unfocused. Some views change their appearance when they are focused. This can be especially useful when using a keypad to navigate the layout. This way you know where you are before you actually click a view. Even views that do not naturally change when focus can be made to do so by using a selector and drawables. If you are not using a keypad to navigate, though, focus is not as important.

视图可以聚焦或不聚焦。某些视图在聚焦时会改变其外观。这在使用键盘导航布局时特别有用。通过这种方式,您可以在实际单击视图之前知道自己的位置。通过使用选择器和可绘制对象,即使焦点不会自然改变的视图也可以这样做。但是,如果您不使用键盘进行导航,则焦点就没有那么重要了。

There were three pairs of things that were confusing me:

有三对事情让我感到困惑:

isFocusable()
isFocusableInTouchMode()

setFocusable()
setFocusableInTouchMode()

requestFocus()
requestFocusFromTouch()

The first pair just tells you information about the view. That is, whether or not it is even possible for that view to be focused. You can find out by doing something like this:

第一对只是告诉您有关视图的信息。也就是说,该视图是否有可能被聚焦。您可以通过执行以下操作来了解:

Boolean b = myView.isFocusable();

You are in touch mode after you have touched the screen. So something that may be focusable when you are using the keypad may not be focusable when you are using your fingers. You can check like this:

触摸屏幕后,您处于触摸模式。因此,当您使用键盘时可能可以聚焦的东西在您使用手指时可能无法聚焦。你可以这样检查:

Boolean b = myView.isFocusableInTouchMode();

Like I said, this only tells you information about whether it is even possibleto give the view focus or not. If you want to actually give the view focus, first you have to make it possible to be focused. You can do this with one of the following commands:

就像我说的那样,这只会告诉您有关是否可以设置视图焦点的信息。如果你想真正赋予视图焦点,首先你必须让它成为焦点。您可以使用以下命令之一执行此操作:

myView.setFocusable(true);
myView.setFocusableInTouchMode(true);

If you are in touch mode and you call setFocusableInTouchMode(true)then both myView.isFocusable()and myView.isFocusableInTouchMode()will return true. You don't need to call them both. However, if you only call myView.setFocusable(true)then myView.isFocusableInTouchMode()will not be changed.

如果你是在触摸模式,你打电话setFocusableInTouchMode(true)都那么myView.isFocusable()myView.isFocusableInTouchMode()将返回true。您不需要同时调用它们。但是,如果您只调用myView.setFocusable(true)那么myView.isFocusableInTouchMode()不会改变。

Now to finally make the view focused you have to call the following:

现在要最终使视图聚焦,您必须调用以下代码:

myView.requestFocus();

I still don't fully understand requestFocusInTouchMode()because just using requestFocus()worked for me, but the documentationsays about requestFocusInTouchMode():

我仍然不完全理解,requestFocusInTouchMode()因为只是使用requestFocus()对我有用,但文档requestFocusInTouchMode()

Call this to try to give focus to a specific view or to one of its descendants. This is a special variant of requestFocus() that will allow views that are not focuable in touch mode to request focus when they are touched.

调用它以尝试将焦点放在特定视图或其后代之一上。这是 requestFocus() 的一个特殊变体,它将允许在触摸模式下不可聚焦的视图在被触摸时请求焦点。

Finally, it should be noted that Romain Guysaid in this post:

最后,需要注意的是,Romain Guy在这篇文章中说:

A view that's focusable in touch mode has weird interactions and unless you perfectly understand what you are doing you should NOT use it. requestFocus() works, but focus is shown only when the device is not in touch mode. As soon as the user touches the screen, the focus is not displayed anymore. By doing what you are doing you are making your app behave differently from the rest of the system and you risk weird behaviors.

在触摸模式下可聚焦的视图具有奇怪的交互作用,除非您完全了解自己在做什么,否则不应使用它。requestFocus() 有效,但只有在设备未处于触摸模式时才会显示焦点。一旦用户触摸屏幕,焦点就不再显示。通过做你正在做的事情,你会让你的应用程序的行为与系统的其他部分不同,你会冒着奇怪行为的风险。