【问题标题】:Hide keyboard input when activity is created [duplicate]创建活动时隐藏键盘输入[重复]
【发布时间】:2018-08-31 12:48:34
【问题描述】:

我知道这个问题被问了很多次,但我尝试了几种建议的解决方案,但都没有按预期工作。

我有一个 EditText 在我启动该活动时打开键盘,如何阻止它启动键盘。

//Solutions I have tried thus far
//Solution 1
//XML
android:editable ="false" <!--is deprecated & does not work-->

//Solution 2
//XML
android:focusable="false" <!--Can no longer use the EditText -->

//Solution 3
//JAVA
editText.setinputType(InputType.TYPE_NULL); // Works but I can no longer use the edit text to add input

//Solution 4 - Similar to Solution 2
editText.setInputType(0); // Works but I can no longer use the edit text to add input

//Solution 5
//Using InputMethodManager
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(),0); //Does not work

【问题讨论】:

  • 在 Activity 下的 Manifest 中添加 android:windowSoftInputMode="stateHidden"

标签: android input android-edittext keyboard


【解决方案1】:

试试这个在清单文件中使用android:windowSoftInputMode="stateHidden" 到您的活动中,如下代码所示

"stateHidden"

当用户选择 Activity 时,软键盘被隐藏——也就是说,当用户肯定地向前导航到该 Activity 时,而不是因为离开另一个 Activity 而返回它。

SAPME 代码

    <activity android:name=".MainActivity"
        android:windowSoftInputMode="stateHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

【讨论】:

    【解决方案2】:
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    

    这对我有用

    【讨论】:

      【解决方案3】:

      在创建时添加这一行

       // Hide the keyboard.
          getWindow().setSoftInputMode(
                  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
          );
      

      【讨论】:

        【解决方案4】:

        请在你的清单上试试这个:

        <activity
                android:name="com.snapit.up.LoginActivity"
                android:screenOrientation="portrait"
                android:windowSoftInputMode="adjustPan|stateHidden" />
        

        它可以帮助你。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-02
          • 1970-01-01
          • 2012-05-10
          • 2014-04-07
          • 2014-11-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多