gpt4 book ai didi

android - 无法设置PositiveButton

转载 作者:行者123 更新时间:2023-11-29 15:18:34 24 4
gpt4 key购买 nike

如果 GPS 关闭,我想在启动器 Activity 之前应用程序启动后立即显示带有两个按钮“启用 GPS”和“取消”的对话框,否则如果 GPS 打开,启动器 Activity 将打开。当用户单击“启用 GPS”,它将进入 GPS 设置,当用户单击“取消”时,我想显示另一个标题为“如何使用应用程序”的对话框,按钮为“确定”,当用户单击“确定”时,我希望启动器 Activity 被打开。为此,我使用以下代码:

    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 显示错误。

最佳答案

此代码可 100% 正常工作,只要您喜欢就可以了。 c 作为上下文传递的所有这些。在您的 Activity 中声明这一点,并将 Activity 名称替换为您的 Activity 名称:

上下文 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();

关于android - 无法设置PositiveButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21377792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com