简体   繁体   English

如何解除对话框关闭时的android键盘?

[英]How to dismiss android keyboard on dialog dismiss?

I have a dialog box open in my android app, and I have a button when clicked will dismiss the dialog box. 我在我的Android应用程序中打开了一个对话框,点击后我有一个按钮将关闭对话框。 The problem is there is also a textedit field, and if its focused and the keyboard is showing, then when I click the cancel button, then dialog goes away, but the keyboard is still showing. 问题是还有一个textedit字段,如果它的焦点和键盘显示,那么当我单击取消按钮,然后对话框消失,但键盘仍然显示。

I want to also dismiss the keyboard. 我也想解雇键盘。

I was searching around, and for threads like this Hide soft keyboard after dialog dismiss 我正在四处搜索,对于这样的线程, 在对话框解除后隐藏软键盘

But none of the solutions worked for me. 但是没有一个解决方案适合我。 By the way the edittext is a number input type, if that makes a difference somehow. 顺便说一句,edittext是一个数字输入类型,如果这会以某种方式产生差异。

Does anyone know how to fix this? 有谁知道如何解决这一问题?

Thanks 谢谢

public void HandleTeamManagement() {
    final Dialog teamDialog = new Dialog(this);
    teamDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    teamDialog.setContentView(R.layout.dialog_team_management);

    final EditText mergeNum = (EditText) teamDialog.findViewById(R.id.group);


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



    // Setting Negative "NO" Button
    Button cancelButton = (Button) teamDialog.findViewById(R.id.cancel);
    cancelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            teamDialog.dismiss();
        }
    });

    // Showing Alert Dialog
    teamDialog.show();
}

You can find a solution here: 你可以在这里找到解决方案:

http://www.workingfromhere.com/blog/2011/04/27/close-hide-the-android-soft-keyboard/ http://www.workingfromhere.com/blog/2011/04/27/close-hide-the-android-soft-keyboard/

Close/hide the Android Soft Keyboard 关闭/隐藏Android软键盘

Edited: adding code Try this ..it worked for me 编辑:添加代码尝试这.. ..为我工作

            Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if (editText!= null && getActivity() != null) {
                InputMethodManager imm = (InputMethodManager) getActivity()
                        .getSystemService(
                                Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(
                        editText.getWindowToken(), 0);
            }
        }
    }, 1000);

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

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