touchscreen属性与焦点(actionabr/toolbar )

现在越来越多的功能机是采用android系统来做的,因为是按键机,所以有很多地方需要调整。

比如本文说的部分机器可能希望,actionbar/toolbar无法获取焦点,觉得按键机有menu建,没必要让actionbar/toolbar上面的menu按钮再获取焦点了。


正文:

1.google定义:touchscreen

frameworks/base/core/res/res/values/attrs.xml

<!-- Set to true if this ViewGroup blocks focus in the presence of a touchscreen. -->
       <attr name="touchscreenBlocksFocus" format="boolean" />
这里看到,在touchscreen情况下,如果设置了这个属性为true,blocksfoucs也就是无法获取焦点。

通过按键是无法将焦点切换到viewgroup上去的。

2.来看看google设计toolbar

/frameworks/base/core/res/res/values/styles.xml
 <style name="Widget.Toolbar">
        <item name="titleTextAppearance">@style/TextAppearance.Widget.Toolbar.Title</item>

        <item name="touchscreenBlocksFocus">true</item>
    </style>
toolbar设置了true,自然也就blcokfocus了。

这就是一些按着系统的按键机的actionbar/toolbar不能通过案件将焦点切上的原因了。

PS:强制修改

方案1:将这些true改成false;比较麻烦,v7包中的也要该,其他的res下面的文件可能也有,也需要修改;

方案2:在系统配置文件目录下面(device/目录/项目.mk),增加文件

android.hardware.touchscreen.xml
<permissions>
    <!-- Disable touch screen features
    <feature name="android.hardware.touchscreen" />
    <feature name="android.hardware.faketouch" />
    -->
</permissions>
然后再在编译文件中加入这个文件,在系统编译的时候能够得到编译从而其作用
PRODUCT_COPY_FILES := \
        $(BOARDDIR)/android.hardware.touchscreen.xml:system/etc/permissions/android.hardware.touchscreen.xml \
        $(PRODUCT_COPY_FILES)
方案2就是将touchscreen给禁了,所以设置了也没用了。

强制修改手机方法:

1.root手机,

2.删除/system/etc/permissions/目录下的文件:android.hardware.touchscreen.xml

(ubuntu系统:adb shell rm -rf /system/etc/permissions/android.hardware.touchscreen.xml)

3.重启手机,可以看到toolbar和actionbar可以获取焦点了。

以上这些xml的属性,如何在代码中控制从而其作用的:具体没有验证,应该是这里控制的。

frameworks/base/core/java/android/view/ViewGroup.java
1122    /**
1123     * Set whether this ViewGroup should ignore focus requests for itself and its children.
1124     * If this option is enabled and the ViewGroup or a descendant currently has focus, focus
1125     * will proceed forward.
1126     *
1127     * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen
1128     */
1129    public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) {
1130        if (touchscreenBlocksFocus) {
1131            mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1132            if (hasFocus()) {
1133                final View focusedChild = getDeepestFocusedChild();
1134                if (!focusedChild.isFocusableInTouchMode()) {
1135                    final View newFocus = focusSearch(FOCUS_FORWARD);
1136                    if (newFocus != null) {
1137                        newFocus.requestFocus();
1138                    }
1139                }
1140            }
1141        } else {
1142            mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS;
1143        }
1144    }





  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值