当使用ActionBar返回时,Android软键盘不会消失。

3

我正在搜索活动中使用 Android 搜索小部件进行搜索。它会自动获取焦点并显示软输入键盘。

然而,当用户使用 ActionBar 返回(但不是返回按钮)时,软输入键盘仍停留在屏幕上,即使活动已经关闭。

android:windowSoftInputMode="stateHidden|adjustUnspecified"

正如https://developer.android.com/training/keyboard-input/visibility.html所描述的那样,它似乎只在前进时起作用,而不是回退时。

问题:可能有许多活动调用SearchActivity,对于它们来说,使stateAlwaysHidden可能不合适。(换句话说:我不知道所有其他活动的行为。)

更新:给出代码,实际上只是actionBar.setDisplayHomeAsUpEnabled(true);

@Override
protected void onCreate(Bundle savedInstanceState) {
    ....
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // get the action bar
        ActionBar actionBar = getActionBar();
        if (null!=actionBar){
            // Enabling Back navigation on Action Bar icon
            actionBar.setDisplayHomeAsUpEnabled(true);          
        }
    }        
    ....
}

更新2:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
   return super.onOptionsItemSelected(item);
}

在不使用后退按钮的情况下,发布代码以返回上一页。 - Rod_Algonquin
它是 actionBar.setDisplayHomeAsUpEnabled(true); - Paul Verest
你把 setDisplayHomeAsUpEnabled 的操作放在哪里了?请发布你的 onOptionsItemSelected。 - Rod_Algonquin
这只是调用super。然而,无论选择了哪个项目,onOptionsItemSelected都是隐藏软输入的好地方... - Paul Verest
1个回答

1
使用Rod引导和SO答案
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    // fix bug when soft-input stays on the screen when navigating away with ActionBar home/back button
    // https://dev59.com/vHNA5IYBdhLWcg3wC5Xh
    //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); had no affect
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    //check if no view has focus:
    View view = this.getCurrentFocus();
    if(view != null){
        //imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);      
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
    return super.onOptionsItemSelected(item);
}

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