android-需要防止我的AutoCompleteTextView在Activity开始时获得关注
作者:互联网
我的XML文件中有一个AutoCompleteTextView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.aashima.premiumpoint.PremiumPoint">
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchByName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:background="#ddd"
android:hint="Search By Name"
android:textColorHint="#fff"
android:gravity="center"
android:layout_alignTop="@+id/searchImageButton"
android:layout_alignBottom="@+id/searchImageButton"
android:layout_toLeftOf="@+id/searchImageButton"
android:textColor="#fff"
android:completionThreshold="1"
android:imeOptions="actionDone"
android:dropDownWidth="fill_parent"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/searchImageButton"
android:src="@drawable/search40"
android:layout_alignParentRight="true"
android:padding="2dp"
android:background="@color/material_blue_grey_950"/>
</RelativeLayout>
我不希望我的活动开始时获得关注.
我尝试将以下内容提供给父级布局:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
在我的AutoCompleteTextView上方提供一个虚拟项目:
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
和设置:
android:nextFocusUp="@id/searchByName"
android:nextFocusLeft="@id/searchByName"
到AutoCompleteTextView.
在搜索中,我发现了类似于以下内容的结果:
android:windowSoftInputMode="stateHidden"
android:windowSoftInputMode="stateUnchanged"
但是我想要的是防止它变得聚焦并且不隐藏键盘.
我没有运气就尝试了许多替代方案.我在这里做错什么了吗?
提前致谢.
编辑:
也尝试过:
clearFocus();
requestFocus();
解决方法:
尝试结合以下两种方法:
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchByName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:background="#ddd"
android:hint="Search By Name"
android:textColorHint="#fff"
android:gravity="center"
android:layout_alignTop="@+id/searchImageButton"
android:layout_alignBottom="@+id/searchImageButton"
android:layout_toLeftOf="@+id/searchImageButton"
android:textColor="#fff"
android:completionThreshold="1"
android:imeOptions="actionDone"
android:dropDownWidth="fill_parent"
android:nextFocusUp="@id/searchByName"
android:nextFocusLeft="@id/searchByName"/>
现在,将虚拟View和您的AutoCompleteTextView带到您的Java代码中并使用:
dummy.requestFocus();
search.clearFocus();
反之亦然.
标签:autocompletetextview,focus,android 来源: https://codeday.me/bug/20191120/2040730.html