Java 防止键盘在活动开始时显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9732761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:51:57  来源:igfitidea点击:

Prevent the keyboard from displaying on activity start

javaandroidfocusandroid-keypad

提问by Fcoder

I have an activity with an Edit Textinput. When the activity is initialized, the Android keyboard is shown. How can the keyboard remain hidden until the user focuses the input?

我有一个Edit Text输入的活动。初始化活动时,会显示 Android 键盘。在用户聚焦输入之前,键盘如何保持隐藏状态?

采纳答案by Lucas

I think the following may work

我认为以下可能有效

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

I've used it for this sort of thing before.

我以前用它做这种事情。

回答by Praveenkumar

Try this also -

也试试这个——

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Otherwise, declare in your manifest file's activity -

否则,在清单文件的活动中声明 -

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
          android:label="@string/app_name"
          android:windowSoftInputMode="stateHidden"
          >

If you have already been using android:windowSoftInputModefor a value like adjustResizeor adjustPan, you can combine two values like:

如果您已经使用android:windowSoftInputModeadjustResize或 之类的值adjustPan,则可以组合两个值,例如:

<activity
        ...
        android:windowSoftInputMode="stateHidden|adjustPan"
        ...
        >

This will hide the keyboard whenever appropriate, but pan the activity view in case the keyboard has to be shown.

这将在适当的时候隐藏键盘,但在必须显示键盘的情况下平移活动视图。

回答by dira

Hide it for all activities using the theme

使用主题为所有活动隐藏它

<style name="MyTheme" parent="Theme">
    <item name="android:windowSoftInputMode">stateHidden</item>
</style>

set the theme

设置主题

<application android:theme="@style/MyTheme">

回答by androidify

Try to declare it in menifest file

尝试在清单文件中声明它

<activity android:name=".HomeActivity"
      android:label="@string/app_name"
      android:windowSoftInputMode="stateAlwaysHidden"
      >

回答by SaraVF

If you are using API level 21, you can use editText.setShowSoftInputOnFocus(false);

如果您使用的是 API 级别 21,则可以使用 editText.setShowSoftInputOnFocus(false);

回答by Yuliia Ashomok

//to hide the soft keyboard
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

回答by Philip BH

Function to hide the keyboard.

隐藏键盘的功能。

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);
    }
}

Hide keyboard in AndroidManifext.xml file.

在 AndroidManifext.xml 文件中隐藏键盘。

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="stateHidden">

回答by King of Masses

Add these two properties to your parent layout (ex: Linear Layout, Relative Layout)

将这两个属性添加到您的父布局(例如:线性布局、相对布局)

android:focusable="false"
android:focusableInTouchMode="false" 

It will do the trick :)

它会做的伎俩:)

回答by Dayanand Waghmare

Just Add in AndroidManifest.xml

只需在 AndroidManifest.xml 中添加

<activity android:name=".HomeActivity"  android:windowSoftInputMode="stateHidden">
</activity>

回答by Efe ?ZYER

Best solution for me, paste your class

对我来说最好的解决方案,粘贴你的课程

@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();
}