简体   繁体   English

按下工具栏的向后箭头时键盘保持打开状态

[英]Keyboard remain opens when Toolbar back arrow is pressed

I am following below code:- 我正在下面的代码:-

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {

            finish();
            overridePendingTransition(R.transition.right_in, R.transition.right_out);


        }
        return super.onOptionsItemSelected(item);
    }

In this when my keyboard is open and I press my toolbar back arrrow ,the keyboard remain open and activity got finish. 在这种情况下,当我的键盘打开并且我按工具栏的向后箭头时,键盘保持打开状态,并且活动完成。 I have tried forcefully hiding keyboard in on pause() by callling below method but doesn't look good while transition :- 我试图通过在下面的方法中调用来强制将键盘隐藏在pause()上,但是在过渡时看起来并不好:-

public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.


        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

    }

Try to put in your toolbar back button this code: 尝试在工具栏的“后退”按钮中添加以下代码:

//Hide keyboard when button was clicked.
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);

like this: 像这样:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {

            finish();
            overridePendingTransition(R.transition.right_in, R.transition.right_out);


        }
    //Hide keyboard when button was clicked.
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
        return super.onOptionsItemSelected(item);
    }

Why are you getting android home button with onOptionsItemSelected() ? 为什么使用onOptionsItemSelected()获得android主页按钮? Just do like above : 就像上面一样:

toolbar.setNavigationOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            main = new Intent(SettingActivity.this, MainActivity.class);
            main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
            finish();

            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);

            startActivity(main);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
        }
    });

Hope it will help you, Darkball60 希望对您有帮助,Darkball60

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM