对话框从onOptionsItemSelected内的andro

对话框从onOptionsItemSelected内的andro

本文介绍了警告对话框从onOptionsItemSelected内的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弹出一个警告对话框,当点击我的OptionsMenuItems之一。继续进行操作,如果用户点击是,如果取消任何点击。我只是不知道该怎么办了code。这是我有:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项)
{    开关(item.getItemId()){
    案例R.id.exit:
        this.finish();
        返回true;
    案例R.id.about:
        意图I =新意图(这一点,AboutActivity.class);
        this.startActivity(ⅰ);
    案例R.id.skip:
        AlertDialog.Builder alertbox =新AlertDialog.Builder(本);            //设置为显示该消息
            alertbox.setMessage)(以下简称瑜伽姿势会从现在开始跳过!)显示(。
           alertbox.setCancelable(真);
           alertbox.setNegativeButton(无)。setPositiveButton(OK,新DialogClicklistener(){             //点​​击提示框监听器
            公共布尔的onClick(DialogInterface为arg0,ARG1 INT){
                //按钮被点击
                布尔成功= myDbHelper.setSkip(PoseID);
                SetImageView2(myDbHelper);
                返回成功;
              }           });           //添加一个中立的按钮,警告框,并分配一个点击监听器
           alertbox.setCancelable(真).SET(onCancelListener){              //点​​击提示框监听器
               公共布尔的onClick(DialogInterface为arg0,ARG1 INT){
                //按钮被点击              }
           });
           // 展示下
           alertbox.show();     默认:
            返回super.onOptionsItemSelected(项目);
    }
}


解决方案

以下是直接从我目前工作的应用程序。它带来了一个AlertDialog,然后将用户带到一个不同的活动(在他们输入密码)。请让我知道如果我能澄清任何事情。

编辑:全法现已列入

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    LayoutInflater吹气=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    最终诠释菜单项= item.getItemId();
    如果((菜单项== R.id.survey)||(菜单项== R.id.syncMode)){
        查看布局= inflater.inflate(R.layout.survey_password_dialog,(ViewGroup中)findViewById(R.id.layout_root));
        最终的EditText密码=(EditText上)layout.findViewById(R.id.passwordText);
        AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        builder.setView(布局)
        .setCancelable(假)
        .setPositiveButton(OK,新DialogInterface.OnClickListener(){
            @覆盖
            公共无效的onClick(DialogInterface对话,诠释的id){
            }
        })
        .setNegativeButton(取消,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释的id){
                dialog.cancel();
            }
        });
        最终AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(新DialogInterface.OnShowListener(){
            @覆盖
            公共无效昂秀(DialogInterface对话){
                按钮B = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                b.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(查看视图){
                        如果(password.getText()。的toString()。equalsIgnoreCase(the_password)){
                            如果(菜单项== R.id.survey){
                                意向意图=新意图(AsthmaAppActivity.this,SurveyActivity.class);
                                startActivity(意向);
                                alertDialog.dismiss();
                            }
                            其他{//(菜单项== R.id.syncMode)
                                startActivity(新意图(AsthmaAppActivity.this,SyncMode.class));
                                alertDialog.dismiss();
                            }
                        }
                        其他Toast.makeText(AsthmaAppActivity.this,密码不正确,Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
        。alertDialog.getWindow()setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        alertDialog.show();
    }
    其他{//对话框对飞的应用程序测试设置应用程序参数
        查看布局= inflater.inflate(R.layout.parameter_change,(ViewGroup中)findViewById(R.id.layout_root));
        最终的EditText参数=(EditText上)layout.findViewById(R.id.parameterText);
        AlertDialog.Builder建设者=新AlertDialog.Builder(本);
        builder.setView(布局)
        .setCancelable(假)
        .setPositiveButton(OK,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释的id){
                。字符串parameterString = parameter.getText()的toString();
                如果(parameterString == NULL || parameterString.isEmpty()){
                    testParam = 0.0;
                }
                其他{
                    testParam = Double.parseDouble(parameterString);
                }
            }
        })
        .setNegativeButton(取消,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释的id){
                dialog.cancel();
            }
        })
        。显示();
    }
    回报(super.onOptionsItemSelected(项目));
}

I'm trying to bring up an alert dialog box when one of my OptionsMenuItems is clicked. Proceed with operation if user clicks "yes", cancels if clicks no. I just don't know how to do the code. This is what I have:

@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);
    }
}
解决方案

The following is directly from the app I'm currently working on. It brings up an AlertDialog which then takes the user to a different activity (after they enter a password). Please let me know if I can clarify anything about it.

Edit: whole method now included.

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

这篇关于警告对话框从onOptionsItemSelected内的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:57