以编程方式访问 AlertDialog PositiveButton

Ric*_*r32 2 java android android-alertdialog

我正在使用 AlertDialog 要求用户在长按视图时输入数值。这使用 Android 软键盘。为了获得更好的用户体验,我希望键盘“Enter”按钮以编程方式单击警报对话框的肯定按钮并运行它的 onClick。这真的很尴尬,因为我在对话框对象中找不到任何对肯定按钮的引用。代码来说明:

    customStakeView.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Custom Stake");
            customStakeSet = false;

            // Set up the input
            final EditText input = new EditText(context);
            input.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            // Specify the type of input expected;
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                    if(keyEvent.getAction() == KeyEvent.ACTION_DOWN){
                        if(!input.getText().toString().equals("")){
                            switch (keyCode){
                                case KeyEvent.KEYCODE_ENTER:
                                    //Positive Button Outcome
                            }
                        }
                    }
                    return false;
                }
            });
            builder.setView(input);

            // Set up the buttons
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String newStake = input.getText().toString();
                    if (!newStake.equals("")) {
                        newStake = newStake.replaceAll("[^\\d.]", "");  //strip down to currency format
                        customStake = new Stake(newStake);
                        customStakeSet = true;

                        deselectAll();
                        selectCustomStake();
                        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(input.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.show();

            return true;
        }
    });
Run Code Online (Sandbox Code Playgroud)

我已经捕获了 KeyEvent,如果这是一个通过 XML 添加的按钮,或者甚至是用变量定义的,我将很容易能够做到

button.performClick();
Run Code Online (Sandbox Code Playgroud)

但AlertDialog似乎没有这样的参考

编辑:

根据建议更新了代码

rup*_*eet 5

文档中,使用getButton(whichButton)

获取对话框中使用的按钮之一。如果指定的按钮不存在或对话框尚未完全创建(例如,通过 show() 或 create()),则返回 null。

whichButton可以是BUTTON_POSITIVE或您指定的任何其他按钮。

下面是它的屏幕截图。

截屏

您没有捕获.create()方法返回的 AlertDialog。getButton()不可用于构建器,但可用于 AlertDialog 对象。

builder.setPositiveButton(...);

// you're missing this
final AlertDialog alertDialog = builder.create();

// then, use it like this
alertDialog.getButton(DialogInterface.Button_POSITIVE).performClick();
Run Code Online (Sandbox Code Playgroud)