我有一个编辑文本输入的活动。当activity初始化时,显示Android键盘。在用户聚焦输入之前,键盘如何保持隐藏?
我认为以下方法可能有用
getWindow().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"
...
>
这将在适当的时候隐藏键盘,但在必须显示键盘的情况下平移活动视图。
为所有使用该主题的活动隐藏它
<style name="MyTheme" parent="Theme">
<item name="android:windowSoftInputMode">stateHidden</item>
</style>
设定主题
<application android:theme="@style/MyTheme">
尝试在manifest文件中声明它
<activity
android:name=".HomeActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysHidden" >
//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
函数隐藏键盘。
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:focusable="false"
android:focusableInTouchMode="false"
它会成功的:)
只需添加AndroidManifest.xml即可
<activity android:name=".HomeActivity" android:windowSoftInputMode="stateHidden">
</activity>
对我来说最好的解决办法是,粘贴你的类
@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();
}
你也可以在你有“问题”的.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不能直接对焦。 再见!: -)
只要在你的活动中添加这个:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
return super.dispatchTouchEvent(ev);
}
扩展一下@Lucas的回答:
从生命周期早期事件的活动中调用这个:
getWindow () .setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
芬兰湾的科特林的例子:
override fun onResume() {
super.onResume()
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
}
只需将其添加到manifest.xml文件中
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden">
你都完成了。
您可以尝试为每个元素设置唯一属性
TextView mtextView = findViewById(R.id.myTextView);
mtextView.setShowSoftInputOnFocus(false);
当元素是焦点时,键盘将不显示
(android:windowSoftInputMode="stateAlwaysHidden") 在活动标签内的清单中。
像这样:
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
只有这个解决方案对我的API 26和Kotlin有效
override fun onResume() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
super.onResume()
}
推荐文章
- 在maven中安装mvn到底做什么
- 在android中从上下文获取活动
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 如何在Java中打印二叉树图?
- 按钮背景是透明的
- String.format()在Java中格式化双重格式
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- com.jcraft.jsch.JSchException: UnknownHostKey