我有一个编辑文本输入的活动。当activity初始化时,显示Android键盘。在用户聚焦输入之前,键盘如何保持隐藏?


当前回答

尝试在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);

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

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

它会成功的:)

或者你可以在xml中使用focusable标签。

Android:focusable=“false”

设置为false。下面是代码片段

函数隐藏键盘。

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

我认为以下方法可能有用

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

我以前用过这种方法。