我有一个编辑文本输入的活动。当activity初始化时,显示Android键盘。在用户聚焦输入之前,键盘如何保持隐藏?
当前回答
扩展一下@Lucas的回答:
从生命周期早期事件的活动中调用这个:
getWindow () .setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
芬兰湾的科特林的例子:
override fun onResume() {
super.onResume()
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
其他回答
您可以尝试为每个元素设置唯一属性
TextView mtextView = findViewById(R.id.myTextView);
mtextView.setShowSoftInputOnFocus(false);
当元素是焦点时,键盘将不显示
对我来说最好的解决办法是,粘贴你的类
@Override
public void onResume() {
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
super.onResume();
}
@Override
public void onStart() {
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
super.onStart();
}
//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
你也可以在你有“问题”的.xml布局文件的直接父布局中编写这些代码行:
android:focusable="true"
android:focusableInTouchMode="true"
例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
...
android:focusable="true"
android:focusableInTouchMode="true" >
<EditText
android:id="@+id/myEditText"
...
android:hint="@string/write_here" />
<Button
android:id="@+id/button_ok"
...
android:text="@string/ok" />
</LinearLayout>
编辑:
例如,EditText包含在另一个布局中:
<?xml version="1.0" encoding="utf-8"?>
<ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
... > <!--not here-->
... <!--other elements-->
<LinearLayout
android:id="@+id/theDirectParent"
...
android:focusable="true"
android:focusableInTouchMode="true" > <!--here-->
<EditText
android:id="@+id/myEditText"
...
android:hint="@string/write_here" />
<Button
android:id="@+id/button_ok"
...
android:text="@string/ok" />
</LinearLayout>
</ConstraintLayout>
关键是要确保EditText不能直接对焦。 再见!: -)
为所有使用该主题的活动隐藏它
<style name="MyTheme" parent="Theme">
<item name="android:windowSoftInputMode">stateHidden</item>
</style>
设定主题
<application android:theme="@style/MyTheme">