做一个输入框时发现android中ditView的默认焦点了,这种问题如果是在输入框还好,但在搜索页面或浏览页面这样就会影响用户体验了,那要如何取消EditView的默认焦点呢,下面我们来看看。

在网上找了好久,有点 监听软键盘事件,有点 调用 clearFouse()方法,但是测试了都没有! xml中也找不到相应的属性可以关闭这个默认行为

解决之道:在EditText的父级控件中找一个,设置成

代码如下

android:focusable="true"

android:focusableInTouchMode="true"

这样,就把EditText默认的行为截断了!

代码如下

style="@style/FillWrapWidgetStyle"

android:orientation="vertical"

android:background="@color/black"

android:gravity="center_horizontal"

android:focusable="true"

android:focusableInTouchMode="true"

>

android:id="@+id/logo"

style="@style/WrapContentWidgetStyle"

android:background="@drawable/dream_dictionary_logo"

/>

style="@style/FillWrapWidgetStyle"

android:background="@drawable/searchbar_bg"

android:gravity="center_vertical"

>

android:id="@+id/searchEditText"

style="@style/WrapContentWidgetStyle"

android:background="@null"

android:hint="Search"

android:layout_marginLeft="40dp"

android:singleLine="true"

/>

查阅了很多资料后,发现以下方法最简单:

在xml中,在EditText控件之前

加入

代码如下

android:id="@+id/linearLayout_focus"

android:focusable="true"

android:focusableInTouchMode="true"

android:layout_width="0px"

android:layout_height="0px"/>

这是一个虚假的LinearLayout,不会显示的,但是会抢走焦点

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐