Android studio 取消对话框时软键盘未关闭

Android studio 取消对话框时软键盘未关闭,android-studio,android-edittext,android-dialog,Android Studio,Android Edittext,Android Dialog,在我的应用程序中,我有一个对话框,用户应该在编辑文本中输入一些文本。但当我点击对话框外部关闭对话框时,对话框关闭,但由于我点击编辑文本而弹出的软键盘保持不变。这很奇怪:当我将WindowsOfInputMode设置为StateAllwayShidden时,键盘变得有点透明,但它没有关闭。我只在纵向上有这个问题,在横向上没有,但那可能是因为软键盘占据了整个屏幕。我也不能点击键盘上的键,因为它没有反应。我已经尝试将WindowsOfInputMode设置为不同的值,并在拨号上设置了一个oncance

在我的应用程序中,我有一个对话框,用户应该在编辑文本中输入一些文本。但当我点击对话框外部关闭对话框时,对话框关闭,但由于我点击编辑文本而弹出的软键盘保持不变。这很奇怪:当我将WindowsOfInputMode设置为StateAllwayShidden时,键盘变得有点透明,但它没有关闭。我只在纵向上有这个问题,在横向上没有,但那可能是因为软键盘占据了整个屏幕。我也不能点击键盘上的键,因为它没有反应。我已经尝试将WindowsOfInputMode设置为不同的值,并在拨号上设置了一个oncancellistener,它应该关闭软键盘,但没有。在我看来,这是一只虫子

我的对话框的代码

public void create(View view) {

        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_SWIPE_TO_DISMISS);
        dialog.setContentView(R.layout.dialoglayout);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
            @Override
            public void onCancel(DialogInterface dialog) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);

                View view = getCurrentFocus();

                if (view == null) {
                    view = new View(getBaseContext());
                }
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        });
        editText = dialog.findViewById(R.id.levelname);
        editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    name = editText.getText().toString();
                    callables.totaloverwriteFile(name,getApplicationContext(),"newlevelname");
                    getApplicationContext().startActivity(new Intent(getApplicationContext(), CreatorActivity.class));
                    return true;
                }
        return true;
            }
        });


    }```

尝试从上下文构建InputMethodManager,类似这样(kotlin)

val inputMethodManager=context?.getSystemService()
inputMethodManager?.hideSoftInputFromWindow(view.windowToken,0)

尝试从上下文构建InputMethodManager,类似这样(kotlin)

val inputMethodManager=context?.getSystemService()
inputMethodManager?.hideSoftInputFromWindow(view.windowToken,0)

i问题刚刚消失,我正在尝试你的答案,但没有成功。当我把它放回有问题的原始代码并运行它时,我突然发现我没有问题,我检查了代码,代码完全一样。但是问题解决了,所以我很高兴(:我问题消失了,我尝试了你的答案,但它不起作用。当我把它放回我遇到问题的原始代码并运行它时。不知从哪里我没有问题,我检查了代码,它完全相同。但是问题解决了,所以我很高兴(:
    val inputMethodManager = context?.getSystemService<InputMethodManager>()
    inputMethodManager?.hideSoftInputFromWindow(view.windowToken,0)