简体   繁体   English

登录表单:键盘在单击按钮时隐藏,但在单击编辑文本后不再显示

[英]Login Form: Keyboard hides on button click, but does not show again after click on edittext

I have a login form with edittexts and a login button(connect).我有一个带有编辑文本和登录按钮(连接)的登录表单。 When a user click the edittexts the keyboard opens, and when they click the login button(connect) they keyboard disappears.当用户单击编辑文本时,键盘会打开,当他们单击登录按钮(连接)时,键盘会消失。 The problem is if a user clicks on a edittext after clicking the login button, the keyboard shows for a second, then dissapears.问题是如果用户在单击登录按钮后单击编辑文本,键盘会显示一秒钟,然后消失。 Using rootview to calculate if the keyboard is showing or not, since that seems to be the easiest way according to stackoverflow.使用 rootview 计算键盘是否显示,因为根据 stackoverflow,这似乎是最简单的方法。 How do i make it so the keyboard can be shown after clicking the button?我如何制作它以便在单击按钮后可以显示键盘?

Onclicklistener点击监听器

boolean clicked=false;

        connect.setOnClickListener(v -> {


        clicked=true;
        keyboard();

    });

keyboard method键盘法

    void keyboard(){

    InputMethodManager inputManager = (InputMethodManager)
            getSystemService(Context.INPUT_METHOD_SERVICE);

        final View activityRootView = findViewById(R.id.wrap);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                activityRootView.getWindowVisibleDisplayFrame(r);

                if (clicked) {
                    int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
                    if (heightDiff > 0.25 * activityRootView.getRootView().getHeight()) {
                        inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            }
        });


}

Your onGlobalLayout listener is called with each layout update, which will lead the keyboard to be dismissed after each layout update if the button was clicked once i don't really get what you're trying to do there and why are you doing this check每次布局更新时都会调用您的 onGlobalLayout 侦听器,如果单击该按钮后每次布局更新后都会导致键盘关闭

if (heightDiff > 0.25 * activityRootView.getRootView().getHeight())

You could simply dismiss the keyboard directly in your ButtonClickListener您可以直接在 ButtonClickListener 中关闭键盘

connect.setOnClickListener(v -> { 
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);        });

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

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