以编程方式访问AlertDialog PositiveButton

时间:2018-01-10 11:03:04

标签: java android alertdialog

我正在使用AlertDialog要求用户在长按视图时输入数值。这使用Android软键盘。为了获得更好的用户体验,我希望键盘上的“Enter”按钮以编程方式单击Alert Dialog的正向按钮并运行它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;
        }
    });

我抓住了KeyEvent,如果这是一个通过XML添加的按钮,或者甚至用变量定义,我很容易就能做到

button.performClick();

但是AlertDialog似乎没有这样的引用

编辑:

Updated code as per suggestion

1 个答案:

答案 0 :(得分:3)

documentation开始,使用getButton(whichButton)

  

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

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

下面是它的截图。

screen-shot

您没有捕获.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();