如何在活动开始时隐藏软键盘

162
我在Manifest中使用了android:windowSoftInputMode="stateVisible"属性的Edittext。现在当我启动这个Activity时,键盘会自动弹出。如何隐藏它?我不能使用android:windowSoftInputMode="stateHidden",因为当键盘可见时,最小化应用程序并恢复它后,键盘应该保持可见状态。 我尝试过以下代码,但是它没有起作用:

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

23个回答

6

如果您不想使用XML,可以创建Kotlin扩展来隐藏键盘

// In onResume, call this
myView.hideKeyboard()

fun View.hideKeyboard() {
    val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}

根据使用场景的不同,有以下几种选择:

fun Fragment.hideKeyboard() {
    view?.let { activity?.hideKeyboard(it) }
}

fun Activity.hideKeyboard() {
    // Calls Context.hideKeyboard
    hideKeyboard(currentFocus ?: View(this))
}

fun Context.hideKeyboard(view: View) {
    view.hideKeyboard()
}

如何显示软键盘
fun Context.showKeyboard() { // Or View.showKeyboard()
    val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}

同时请求EditText焦点的更简单方法

myEdittext.focus()

fun View.focus() {
    requestFocus()
    showKeyboard()
}

奖励简化:

无需使用getSystemService:使用Splitties Library

// Simplifies above solution to just
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)

5

为了在新活动启动或onCreate(),onStart()等时隐藏软键盘,请使用以下代码:

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

在活动中点击按钮时隐藏软键盘:
View view = this.getCurrentFocus();

    if (view != null) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        assert imm != null;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

5

使用SOFT_INPUT_STATE_ALWAYS_HIDDEN代替SOFT_INPUT_STATE_HIDDEN

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

5
把这段代码放在你的Java文件中,并将对象参数传递给EditText:
private void setHideSoftKeyboard(EditText editText){
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}

5
以上答案也是正确的。我只想简要说明一下,从manifest.xml有两种方法隐藏启动活动时的键盘。 例如:
<activity
..........
android:windowSoftInputMode="stateHidden"
..........
/>
  • 以上方法总是在进入活动时隐藏它。

或者

<activity
..........
android:windowSoftInputMode="stateUnchanged"
..........
/>
  • 这个表示不要改变它(例如,如果它没有显示,请不要显示它,但如果在进入活动时已经打开,请保持它打开)。

4
你可以在AndroidManifest.xml中设置配置。
示例:
<activity
    android:name="Activity"
    android:configChanges="orientation|keyboardHidden"
    android:theme="@*android:style/Theme.NoTitleBar"
    android:launchMode="singleTop"
    android:windowSoftInputMode="stateHidden"/>

4
使用以下代码,在您启动活动时首次隐藏软键盘
getActivity().getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_HIDDEN);

3
这是我所做的事情:
yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup
        yourEditText.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);   // handle the event first
                InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {

                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  // hide the soft keyboard
                    yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext
                }
                return true;
            }
        });

3

也可以尝试这个

Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories);

Ed_Cat_Search.setInputType(InputType.TYPE_NULL);

Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT);
        Ed_Cat_Search.onTouchEvent(event); // call native handler
        return true; // consume touch even
    }
});

2

如果您的应用程序针对的是Android API Level 21或更高版本,那么就有一个默认方法可用。

editTextObj.setShowSoftInputOnFocus(false);

请确保您已在EditText的XML标签中设置了以下代码。
<EditText  
    ....
    android:enabled="true"
    android:focusable="true" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接