AlertDialog with OK/Cancel buttons

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-24 03:12:25

问题


I create this AlertDialog:

            String msg = "Connessione lenta o non funzionante";
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(HomePage.this).create();
            alertDialog.setTitle("Timeout connessione");
            alertDialog.setMessage(msg);
            alertDialog.show();

I want to add OK and Cancel buttons. I searched here on StackOverflow but setButton method seems to be deprecated. I also found setPositiveButton and setNegativeButton for AlertDialog.Builder but even them seem to be deprecated.


回答1:


You can use AlertDialog.Builder.setPositiveButton and AlertDialog.Builder.setNegativeButton, both are not deprecated (see the documentation):

new AlertDialog.Builder(HomePage.this)
        .setTitle("Timeout connessione")
        .setMessage("Connessione lenta o non funzionante")
        .setNegativeButton(android.R.string.cancel, null) // dismisses by default
        .setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override public void onClick(DialogInterface dialog, int which) {
                // do the acknowledged action, beware, this is run on UI thread
            }
        })
        .create()
        .show();



回答2:


Use alertDialog.setPositiveButton and alertDialog.setNegativeButton, Here is a Utility Method you can use:

public static void ask(final Activity activity, String title, String msg,
                       DialogInterface.OnClickListener okListener, 
                       DialogInterface.OnClickListener cancelListener) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
    alertDialog.setTitle(title);
    alertDialog.setMessage(msg);
    alertDialog.setPositiveButton(R.string.ok, okListener);
    alertDialog.setNegativeButton(R.string.cancel, cancelListener);
    alertDialog.show();
}

You can call it like this:

ask(mInstance, getString(R.string.app_name),
            getString(R.string.confirm_text_here),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { // OK
                    // do Something
                }
            }, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { // Cancel
                    // do Something
                }
            });

For more details refer Android Docs




回答3:


I usually use this code and It works perfectly use this  

  DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        finish();
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        break;
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Want To Do Some Task").setPositiveButton("Ok"), dialogClickListener)
            .setNegativeButton("Cancel"), dialogClickListener).show();



回答4:


try this code i think it will work perfectly.

public void open(View view){
  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
  alertDialogBuilder.setTitle("alert dialog");
  alertDialogBuilder.setMessage("conform to delete");
  alertDialogBuilder.setPositiveButton("ok", 
  new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface arg0, int arg1) {
        Intent positveActivity = new                                                 Intent(getApplicationContext(),com.example.alertdialog.PositiveActivity.class);
        startActivity(positveActivity);

     }
  });
  alertDialogBuilder.setNegativeButton("cancel", 
  new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
        Intent negativeActivity = new Intent(getApplicationContext(),com.example.alertdialog.NegativeActivity.class);
        startActivity(negativeActivity);
     }
  });

  AlertDialog alertDialog = alertDialogBuilder.create();
  alertDialog.show();
    }


来源:https://stackoverflow.com/questions/25504458/alertdialog-with-ok-cancel-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!