如何阻止EditText在Android活动启动时获得关注
我在Android中有一个如何阻止EditText在Android活动启动时获得关注,android,listview,android-edittext,focus,Android,Listview,Android Edittext,Focus,我在Android中有一个活动,包含两个元素: getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); EditText ListView 当我的活动启动时,编辑文本立即具有输入焦点(光标闪烁)。我不希望任何控件在启动时具有输入焦点。我试过: EditText.setSelected(false); EditText.setFocusable(false); getWindow().s
活动
,包含两个元素:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
EditText
ListView
活动
启动时,编辑文本
立即具有输入焦点(光标闪烁)。我不希望任何控件在启动时具有输入焦点。我试过:
EditText.setSelected(false);
EditText.setFocusable(false);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
不走运。如何说服
编辑文本
在活动
启动时不选择自身?尝试而不是设置已选(false)
。Android中的每个视图都具有可聚焦性和可选择性,我认为您只需清除焦点即可。我找到的唯一解决方案是:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
- 创建一个(我不知道其他类型的布局是否可行)
- 设置属性
和android:focusable=“true”
android:focusableInTouchMode=“true”
开始活动后,
EditText
将无法获得焦点是的,我做了同样的事情-创建一个获得初始焦点的“虚拟”线性布局。此外,我设置了“下一个”焦点ID,以便用户在滚动一次后无法再对其进行焦点设置:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
为了摆脱对视图的关注,做了很多工作
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
谢谢来自Luc和Mark的精彩回答,但是缺少一个好的代码示例。将标签
android:focusableInTouchMode=“true”
和android:focusable=“true”
添加到父布局(例如LinearLayout
或ConstraintLayout
)可以解决问题,如下示例所示
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
实际的问题是你根本不想让它有焦点吗?或者,您不想让它显示虚拟键盘,因为它专注于
编辑文本
?我并不认为EditText
在开始时有什么问题,但是当用户没有明确要求关注EditText
(并因此打开键盘)时,打开softInput窗口肯定是个问题
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
如果是虚拟键盘的问题,请参阅AndroidManifest.xml
文档
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
android:windowSoftInputMode=“stateHidden”
-输入活动时始终将其隐藏
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
或android:WindowsOfInputMode=“stateUnchanged”-不要更改它(例如,如果它尚未显示,则不要显示,但如果它在进入活动时已打开,则保持打开状态)。很晚,但可能会有所帮助。在布局的顶部创建一个伪EditText,然后在
onCreate()中调用myDummyEditText.requestFocus()
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
这似乎和我预料的一样。无需处理配置更改等。对于具有冗长文本视图(说明)的活动,我需要此功能。如果您的活动有另一个视图,如列表视图,您还可以执行以下操作:
ListView.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
在onResume()
中,从editText
获取焦点
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
我知道这个问题已经得到了回答,但只是提供了一个对我有用的替代解决方案:)使用其他海报提供的信息,我使用了以下解决方案:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
布局XML中的
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
最后,在onResume()中
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
在您的第一个可编辑字段之前尝试以下操作:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus.requestFocus();
您只需在布局的第一个文本视图上将“可聚焦”和“触摸模式下可聚焦”设置为true。通过这种方式,当活动开始时,文本视图将被聚焦,但由于其性质,您将在屏幕上看不到任何聚焦,当然,将显示无键盘…这些解决方案对我都不起作用。我修复自动对焦的方法是:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
存在一个更简单的解决方案。在父布局中设置以下属性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
好评来自:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
android:genderantfocusability=“beforesandroid”
似乎是
默认值(整数值为0)。它只需添加
android:focusableInTouchMode=“true”
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
实际上,我们可以看到在ViewGroup.initViewGroup()
方法(Android 2.2.2)中,beforemedints
设置为默认值。但不等于0<代码>ViewGroup.FOCUS\u在子体之前=0x20000代码>
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
感谢纪尧姆。
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="@android:style/Widget.EditText"/>
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
问题似乎来自一个属性,我只能在布局的XML表单中看到该属性
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
确保在EditText
XML标记中删除声明末尾的这一行:
<requestFocus />
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
这应该是这样的:
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
///*以下内容将在创建edittext时停止聚焦,但在触摸它们时将其抓取
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
因为您返回的是false,即不使用事件,所以聚焦行为将正常进行。我需要以编程方式从所有字段中清除聚焦。我刚刚在主布局定义中添加了以下两条语句
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
就这样。立即解决了我的问题。谢谢你,西尔弗,为我指出了正确的方向。我已经尝试了几个单独的答案,但重点仍然是文本。我只是通过同时使用下面的两个解决方案来解决它
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
(银币参考号)
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
移除
<requestFocus />
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
编辑文本时
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
(来自floydaddict的参考资料)我做的最简单的事情就是在onCreate中设置另一个视图的焦点:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
这阻止了软键盘的出现,编辑文本中没有光标闪烁。因为我不喜欢用与功能相关的东西污染XML,所以我创建了这个“透明”窃取XML的方法
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);