android:focusable 和 android:focusableInTouchMode 的区别

由于要做日历开发渐进教程(5),而(5)中要完成一个功能:点击任意一天时,高亮显示目标日期

这个功能显然需要用到focus实现,然后就去查。有focusable和focusableInTouchMode,现在搞明白了一些,写下自己的理解,希望你能帮助大家吧。

通过搜索,在stackoverFlow上找到了两个答案,基本解决了我的疑惑。

http://stackoverflow.com/questions/24155956/difference-between-focusable-and-focusableintouchmode

http://stackoverflow.com/questions/23799064/what-is-the-difference-between-setfocusable-and-setfocusableintouchmode

关于焦点是什么的问题?

类似非触屏手机时代,需要使用键盘的上下左右去选中某个应用,然后点击确定执行。而触屏手机,我们只需要对应用点击一次,即可,无需焦点。也就是会所焦点是为了标记你目前选中的位置的。而这个在日历中却是有用的。

android:focusable与android:focusableInTouchMode

前者针对在键盘下操作的情况,如果设置为true,则键盘上下左右选中,焦点会随之移动。

而后者,显然是针对触屏情况下的,也就是我们点击屏幕的上的某个控件时,不要立即执行相应的点击逻辑,而是先显示焦点(即控件被选中),再点击才执行逻辑。

android:focusable=“true”不会改变android:focusableInTouchMode,因此只在键盘状态下显示焦点,在TouchMode状态下,依旧无法显示焦点。

android:focusable=“false”,一定会使android:focusableInTouchMode=“false”。

相对的

android:focusableInTouchMode=“false”,不会影响android:focusable。

android:focusableInTouchMode=”true”,一定会是android:focusable=“true”

 

说来说去有点绕。

但请记住一点,就是对于现在触屏时代的手机而言,如果要获取焦点,我们只需要设置

android:foucusableInTouchMode=“true”就可以了。

 

所有的获取焦点,都要有一个前提,那就是该控件必须设置android:clickable=”true”,如果都点击不了,设置焦点应该没什么意义了吧。


在后续的日历开发中有对focusableInTouchMode的应用,大家可以看到。

可以参考:https://www.cnblogs.com/daishuguang/p/3851830.html


猜你喜欢

转载自blog.csdn.net/it666dhw/article/details/80765481
今日推荐