简体   繁体   English

Android应用程序中的AlertDialog未显示

[英]AlertDialog in Android app not showing

I am attempting to create an alert dialog, but it does not appear when I run my application. 我正在尝试创建一个警报对话框,但是在运行应用程序时它不会出现。 Here is my code: 这是我的代码:

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, final int id) {
            startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }
    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, final int id) {
            dialog.cancel();
            buildLocationMessage();
        }
    });
    final AlertDialog alert = builder.create();
    alert.show();

Try this: 尝试这个:

final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(message);

        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
            }
        });
        alertDialog.show();

Just add the setButton for "No" 只需将setButton添加为“否”

Try this code - 试试这个代码-

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setMessage("Your GPS seems to be disabled,do you want to enable it?");

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) {

//do stuff
startActivity(
     new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
   }
});

builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

AlertDialog alert = builder.create();
alert.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM