【问题标题】:AlertDialog how to close Keyboard after inputsAlertDialog如何在输入后关闭键盘
【发布时间】:2016-04-15 03:33:21
【问题描述】:

无法运行 MainScreen Activity。始终通过add.setOnClickListener 获得NullPointerException

在您的解决方案中,onClickListener 看起来不同,无法理解您的意思。

final EditText input = (EditText) findViewById(R.id.input_name);
        final Button add = (Button) findViewById(R.id.btn_add);

        final Dialog dialog = new Dialog(MainScreen.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.name_prompt);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);

        dialog.show();

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("Clicked");
                dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            }
        });

这是迄今为止我最好的解决方案。唯一的问题是,输入完成后键盘不会自动消失。只有按下返回键才会关闭键盘。

           AlertDialog.Builder builder = new AlertDialog.Builder(this);

        final EditText input = new EditText(getBaseContext());

        input.setTextColor(Color.BLACK);
        input.setHint(R.string.hint_name);
        input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        input.setMaxLines(1);
        input.setFocusable(true);

        builder.setCancelable(false);
        builder.setView(input);

        builder.setPositiveButton("ADD", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {


                if (input.getText().length() == 0)
                    players.add(new Player(0, "Nobody"));
                else
                    players.add(new Player(0, input.getText().toString()));
            }
        });
        builder.show();
        input.requestFocus();

【问题讨论】:

    标签: android crash keyboard hide android-alertdialog


    【解决方案1】:

    好的,只是给你一个想法。

    编辑: 新答案问题1:

    alert.setNegativeButton(cancel,
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                saveimage();
                InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                im.hideSoftInputFromWindow(input.getWindowToken(), 0);
                dialog.cancel();
            }
        });
    

    回答问题2:

    builder.setCancelable(false);
    

    这应该能让你继续前进。

    【讨论】:

    • thx,所以你说我应该创建自己的对话框而不是使用 AlertDialog。明天试试
    • 没错。那么你有更多的控制权。
    • 不幸的是我无法让它工作。编辑了第一个帖子。问题2解决了。谢谢
    猜你喜欢
    • 2019-06-04
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    相关资源
    最近更新 更多