创建活动时隐藏键盘输入

时间:2018-03-22 07:13:13

标签: android input android-edittext keyboard

我知道这个问题已被提出很多,但我尝试了几个解决方案,但没有一个按预期工作。

我有一个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

4 个答案:

答案 0 :(得分:1)

尝试将清单文件中的android:windowSoftInputMode="stateHidden"用于您的活动,如下面的代码

<强> "stateHidden"

  

当用户选择活动时隐藏软键盘 - 也就是说,当用户肯定地向前导航到活动时,而不是因为离开另一个活动而退回到活动中。

SAPME CODE

    <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>

答案 1 :(得分:1)

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

这对我有用

答案 2 :(得分:1)

在创建

上添加此行
 // Hide the keyboard.
    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
    );

答案 3 :(得分:1)

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

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

它会帮助你。