我有一个编辑文本输入的活动。当activity初始化时,显示Android键盘。在用户聚焦输入之前,键盘如何保持隐藏?
当前回答
你也可以在你有“问题”的.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不能直接对焦。 再见!: -)
其他回答
我认为以下方法可能有用
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
我以前用过这种方法。
扩展一下@Lucas的回答:
从生命周期早期事件的活动中调用这个:
getWindow () .setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
芬兰湾的科特林的例子:
override fun onResume() {
super.onResume()
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
试试这个——
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
另外,
您还可以在清单文件中声明activity -
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden"
>
如果你已经使用android:windowSoftInputMode的值,如adjuststresize或adjustPan,你可以组合两个值,如:
<activity
...
android:windowSoftInputMode="stateHidden|adjustPan"
...
>
这将在适当的时候隐藏键盘,但在必须显示键盘的情况下平移活动视图。
只有这个解决方案对我的API 26和Kotlin有效
override fun onResume() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
super.onResume()
}
函数隐藏键盘。
public static void hideKeyboard(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
隐藏键盘在androidmanifest .xml文件。
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:windowSoftInputMode="stateHidden">
推荐文章
- Android是否保留。apk文件?如果有,在哪里?
- 从枚举中选择一个随机值?
- 如何从URL获取参数与JSP
- 如何在Eclipse中生成Javadoc注释?
- WPF和初始焦点
- 找到java类从哪里加载
- 从集合中随机选取一个元素
- 为什么x == (x = y)和(x = y) == x不一样?
- 什么Java 8流。收集等价物可在标准Kotlin库?
- 在Android 5棒棒糖中,通知栏图标变成白色
- 从URI获取真实路径,Android奇巧新的存储访问框架
- 等待未来的名单
- 如何检查JSON键是否存在?
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 即使从未抛出异常,使用try-catch块的代价是否昂贵?