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

wecizke3  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(163)

在我的应用程序中,有一个对话框,用户应该在其中输入一些文本编辑文本。但当我点击对话框外关闭对话框时,对话框关闭,但弹出的软键盘,因为我点击了编辑文本停留。这是非常奇怪的:当我将windowsoftinputmode设置为stateAlwaysHidden时,键盘会变得有点透明,但不会关闭.我只在纵向模式下遇到这个问题,在横向,它不会发生,但这可能是因为软键盘充满了整个屏幕。我也不能点击键盘的键,它没有React。我已经试过将windowsoftinputmode设置为不同的值,我在我的拨号盘上设置了一个oncancellistener,它应该关闭软键盘,但它没有。在我看来这是一个bug。
我的对话框的代码

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;
            }
        });

    }```
68de4m5k

68de4m5k1#

尝试从上下文构建InputMethodManager,类似于以下内容(Kotlin)

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

相关问题