3

我在 SearchActivity 上使用 Android SearchWidget 进行搜索。它会自动获得焦点并显示软输入键盘。

但是,当用户使用 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);
}
4

1 回答 1

1

使用 Rod Lead 和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://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard
    //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);
}
于 2014-08-01T08:09:49.343 回答