如果GPS关闭,我想在启动器活动之前启动应用程序时显示带有“ENABLE GPS”和“CANCEL”两个按钮的对话框,否则如果GPS打开,启动器活动将打开。当用户启动时单击“启用GPS”它将进入GPS设置,当用户单击取消时,我想显示另一个标题为“如何用户应用程序”的对话框,按钮“确定”,当用户点击“确定”时,我希望启动器活动到开了。 为此,我使用以下代码:
if (!isGPSEnabled) {
// no GPS provider is enabled
// Creating the Dialog box
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.create();
alertDialog.setTitle("Settings");
alertDialog.setMessage("Enable GPS for accessing the Application");
alertDialog.setButton("GPS Settings",
new DialogInterface.OnClickListener() {
// creating Button i.e GPS Setting in the Dialog Box
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
alertDialog.show();
// creating Button i.e Cancel in the Dialog Box
alertDialog.setButton2("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// Creating another Dialog when "Cancel" is pressed
AlertDialog whencancelpressed = new AlertDialog.Builder(
MainActivity.this).create();
whencancelpressed.setTitle("How to use the app");
whencancelpressed
.setMessage("Enter the contacts to whom you want to send the message.Double tap power button in emergency");
whencancelpressed.setButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Intent pressedokay = new Intent(
getApplicationContext(),
MainActivity.class);
startActivity(pressedokay);
}
});
whencancelpressed.show();
}
});
} else {
// GPS provider is enabled
}
我面临的问题是,我无法进行“取消”按钮显示,当经过几个教程后,我试图将setButton更改为setPositiveButton,eclipse显示错误。
答案 0 :(得分:3)
此代码100%正常工作,只是随心所欲。所有这些都将c
作为上下文传递。
在您的活动中声明此内容并使用您的活动名称替换活动名称:
Context c = ActivityName.this;
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle(R.string.confirm);
builder.setMessage(R.string.deleteConfirm);
builder.setPositiveButton(R.string.deleteBtnTxt,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.no,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
答案 1 :(得分:2)
尝试在显示Dialog
之前设置取消按钮,如下所示:
if (!isGPSEnabled) {
// no GPS provider is enabled
// Creating the Dialog box
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.create();
alertDialog.setTitle("Settings");
alertDialog.setMessage("Enable GPS for accessing the Application");
alertDialog.setPositiveButton("GPS Settings",
new DialogInterface.OnClickListener() {
// creating Button i.e GPS Setting in the Dialog Box
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
startActivity(new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
});
// creating Button i.e Cancel in the Dialog Box
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// Creating another Dialog when "Cancel" is pressed
AlertDialog whencancelpressed = new AlertDialog.Builder(
MainActivity.this).create();
whencancelpressed.setTitle("How to use the app");
whencancelpressed
.setMessage("Enter the contacts to whom you want to send the message.Double tap power button in emergency");
whencancelpressed.setButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
Intent pressedokay = new Intent(
getApplicationContext(),
MainActivity.class);
startActivity(pressedokay);
}
});
whencancelpressed.show();
}
});
alertDialog.show();
} else {
// GPS provider is enabled
}