【问题标题】:Alert Dialog from within onOptionsItemSelected androidonOptionsItemSelected android中的警报对话框
【发布时间】:2012-10-20 00:42:18
【问题描述】:

当我的一个 OptionsMenuItems 被点击时,我试图打开一个警告对话框。如果用户单击“是”则继续操作,如果单击“否”则取消。我只是不知道如何编写代码。这就是我所拥有的:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{ 

    switch (item.getItemId()) { 
    case R.id.exit:
        this.finish();
        return true;
    case R.id.about:
        Intent i = new Intent(this, AboutActivity.class);
        this.startActivity(i);
    case R.id.skip:
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

            // set the message to display
            alertbox.setMessage("The yoga pose will be skipped from now on!").show();
           alertbox.setCancelable(true);
           alertbox.setNegativeButton("no").setPositiveButton("OK", new DialogClicklistener() { 

             // click listener on the alert box
            public boolean onClick(DialogInterface arg0, int arg1) {
                // the button was clicked
                boolean success = myDbHelper.setSkip(PoseID);
                SetImageView2(myDbHelper);
                return success;
              }

           });

           // add a neutral button to the alert box and assign a click listener
           alertbox.setCancelable(true).set(onCancelListener) {

              // click listener on the alert box
               public boolean onClick(DialogInterface arg0, int arg1) {
                // the button was clicked

              }
           });
           // show it
           alertbox.show();

     default: 
            return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 当前代码有什么问题?
  • 它不会编译。我没有正确的警报代码来正确设置 onClick 行为。
  • 有什么问题?当用户单击“确定”时,您所拥有的似乎应该调用 SetImageView2 。实际上,您似乎不需要 onCancelListener ,所以我会核对那部分代码。不过,您似乎没有创建对话框..
  • 是的,我不知道如何编写警报对话框按钮的代码。这不是正确的代码。 Eclipse显示各种错误,我只是卡住了。
  • 另外,应该有一个选项让用户说,“是的,取消这个”。我认为我们需要它。

标签: android menu dialog option


【解决方案1】:

以下内容直接来自我目前正在开发的应用程序。它会弹出一个 AlertDialog,然后将用户带到不同的活动(在他们输入密码之后)。如果我能澄清任何事情,请告诉我。

编辑:现在包括整个方法。

@Override
public boolean onOptionsItemSelected(MenuItem item) { 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    final int menuItem = item.getItemId();
    if ((menuItem == R.id.survey) || (menuItem == R.id.syncMode)) {
        View layout = inflater.inflate(R.layout.survey_password_dialog, (ViewGroup) findViewById(R.id.layout_root));
        final EditText password = (EditText) layout.findViewById(R.id.passwordText);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);            
        builder.setView(layout)
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {   
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        final AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                b.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (password.getText().toString().equalsIgnoreCase("the_password")) {
                            if (menuItem == R.id.survey) {
                                Intent intent = new Intent(AsthmaAppActivity.this, SurveyActivity.class);
                                startActivity(intent);
                                alertDialog.dismiss();
                            }
                            else { //(menuItem == R.id.syncMode) 
                                startActivity(new Intent(AsthmaAppActivity.this, SyncMode.class));
                                alertDialog.dismiss();
                            }
                        }
                        else Toast.makeText(AsthmaAppActivity.this, "Password incorrect", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
        alertDialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        alertDialog.show();
    }
    else {  //dialog for setting application parameters "on the fly" for application testing
        View layout = inflater.inflate(R.layout.parameter_change, (ViewGroup) findViewById(R.id.layout_root));
        final EditText parameter = (EditText) layout.findViewById(R.id.parameterText);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout)
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {  
                String parameterString = parameter.getText().toString();
                if(parameterString == null || parameterString.isEmpty()) {
                    testParam = 0.0;
                } 
                else {
                    testParam = Double.parseDouble(parameterString);
                }
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        })
        .show();
    }
    return(super.onOptionsItemSelected(item));
} 

【讨论】:

  • 这似乎是我需要的。是的,我很想看看其余的代码。
  • 很高兴为您提供帮助。使用整个方法查看编辑后的答案(使用了 2 个不同的 AlertDialogs,用于不同的菜单项)。
  • 一个问题,你为什么用 .setCancelable(false) 而不是 true?
  • 老实说,我不确定,但我认为在我的情况下,它确保用户无法绕过对话框并且仍然可以在不输入密码的情况下进入活动。该对话框可以通过取消按钮取消,但这只会将人带到他们开始的活动,而不是如果他们正确输入密码并点击确定,他们会去的另一个活动。还要注意,如果该人输入了不正确的密码,祝酒词会告诉他们,但对话框不会消失,因此他们有机会输入正确的密码。 .setCancelable(false) 可能会解决这个问题。
  • 无论我将其设置为 true 还是 false,行为似乎都是一样的。
【解决方案2】:
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
alertbox.setPositiveButton("Yes" new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    log("i'm clicked");
                }
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多