【问题标题】:Prevent the keyboard from displaying on activity start防止键盘在活动开始时显示
【发布时间】:2012-04-01 17:31:29
【问题描述】:

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

【问题讨论】:

标签: java android focus android-keypad


【解决方案1】:

我认为以下可能有效

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

我以前用它来做这种事情。

【讨论】:

  • 有没有办法将其设置为纯数字键盘?即 12 键键盘?
  • @MohamedKhamis input.setRawInputType(Configuration.KEYBOARD_12KEY);
  • 是的,它仍然有效。 @SagarNayak 为什么要为EditText 隐藏键盘?:) 这是在包含EditText的活动开始时隐藏键盘@
  • @Devealte 7 年后它对我有用,你把它放在 onCreate 中了吗?
  • @Dymas 是的,几个月前我刚刚修好了 :)
【解决方案2】:

试试这个 -

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

或者,

  1. 您也可以在清单文件的活动中声明 -
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
          android:label="@string/app_name"
          android:windowSoftInputMode="stateHidden"
          >
  1. 如果您已经将android:windowSoftInputMode 用作adjustResize 或adjustPan 之类的值,则可以组合两个值,例如:
<activity
        ...
        android:windowSoftInputMode="stateHidden|adjustPan"
        ...
        >

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

【讨论】:

  • 感谢您在代码和 xml 中显示!确实,这是最正确的答案!特别是因为你可能花了 3 分钟编写这两种方法,这让你不是第一个 ;-)
【解决方案3】:

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

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

设置主题

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

【讨论】:

  • 喜欢这种全球性的做法。
  • 这很有效,因为我在不同的地方使用了不同的主题
【解决方案4】:

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

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

它会成功的:)

【讨论】:

  • 这对我不起作用,但是根据 Jack T 的回答,将它们设置为 true 有效。最近的版本是否有行为变化?
  • 除了我的回答之外,您还需要检查清单中的其他属性以及编辑文本。
  • 我只有最基本的属性。我不明白为什么将这些设置为 false 应该可以工作,因为这样做是为了让焦点远离 EditText 框。
  • 也许它曾经通过让 EditText 框远离父布局来让焦点远离它?但似乎不再是了。
【解决方案5】:

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

【讨论】:

    【解决方案6】:

    尝试在清单文件中声明它

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

    【讨论】:

      【解决方案7】:

      只需添加 AndroidManifest.xml

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

      【讨论】:

        【解决方案8】:

        您也可以在您遇到“问题”的 .xml 布局文件的 direct 父布局中编写这些代码行:

        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 不能直接聚焦。
        再见! ;-)

        【讨论】:

          【解决方案9】:

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

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

          【讨论】:

            【解决方案10】:

            只需将其添加到您的 manifest.xml 文件中

            <activity android:name=".MainActivity"
                        android:windowSoftInputMode="stateHidden">
            

            你已经完成了。

            【讨论】:

              【解决方案11】:

              扩展@Lucas 接受的答案:

              从您在早期生命周期事件之一中的活动中调用它:

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

              Kotlin 示例:

              override fun onResume() {
                super.onResume()
              
                window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
              }
              

              【讨论】:

                【解决方案12】:

                隐藏键盘的功能。

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

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

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

                【讨论】:

                  【解决方案13】:

                  您可以尝试为每个元素设置唯一属性

                  TextView mtextView = findViewById(R.id.myTextView);
                  mtextView.setShowSoftInputOnFocus(false);
                  

                  当元素为焦点时,键盘不会显示

                  【讨论】:

                  • 虽然这可能会回答作者的问题,但它缺少一些解释性文字和/或文档链接。如果没有围绕它们的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现how to write a good answer 非常有帮助。请编辑您的答案 - From Review
                  【解决方案14】:
                  //to hide the soft keyboard
                  InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                  

                  【讨论】:

                    【解决方案15】:

                    只需将其添加到您的活动中:

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

                    【讨论】:

                      【解决方案16】:

                      声明此代码(android:windowSoftInputMode="stateAlwaysHidden") 在您的活动标签内的清单中。

                      像这样:

                      <activity android:name=".MainActivity"
                        android:windowSoftInputMode="stateAlwaysHidden">
                      

                      【讨论】:

                        【解决方案17】:

                        只有这个解决方案适用于 API 26 和 Kotlin

                           override fun onResume() {
                            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
                            super.onResume()
                        }
                        

                        【讨论】:

                          【解决方案18】:

                          或者您可以在 xml 中使用可聚焦标签。

                          android:focusable="false"

                          将其设置为 false。here is the snippet of the code

                          【讨论】:

                            猜你喜欢
                            • 2011-10-17
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            相关资源
                            最近更新 更多