gpt4 book ai didi

java - 如何修改AlertDialog(Android)?

转载 作者:行者123 更新时间:2023-12-01 21:48:59 24 4
gpt4 key购买 nike

我是 Java 和 Android Studio 的初学者,所以,我的问题是:我为某个 Activity 创建了一个警报对话框窗口,其中正按钮为“确定”,负按钮为“不,谢谢”。如下面的代码所示。

if(Times==0) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setIcon(android.R.drawable.ic_dialog_alert);
builder1.setTitle("Warning");
builder1.setMessage("Rooting of a phone may void your Warranty in most of the cases,so it is adviced to proceed at your own risk");
builder1.setCancelable(true);

builder1.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Times += 1;
dialog.cancel();
}
});

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

}
});
}

一切进展顺利,但现在的问题是,我希望它仅在用户单击“确定”按钮时显示一次,并且不想在用户单击“确定”按钮时再次显示。我在类中创建了一个变量 times 并将其初始化为零,如下所示。

public class rootingRooting extends AppCompatActivity {

int Times=0;

并将完整的AlertDialog放入if循环中,并在用户单击“确定”时递增其值,以便在用户单击“确定”时循环仅执行一次,但它是每当我打开 Activity 时,尽管单击“确定”,但都会显示警报框,但没有用。所以,现在我想做的事情是:

  1. 如果用户单击“确定”,则不应显示警告框。

  2. 如果用户单击“不,谢谢”按钮,我想带他到家庭 Activity 。那么,我应该如何使用带有“不,谢谢”按钮的 Intent ?

谢谢。

最佳答案

您需要使用SharedPreferenes来持久保存数据,局部变量没有帮助。像这样的东西:

编辑根据您的要求,我添加了一个示例 Activity 类来显示整个过程。请参阅中间的评论以获取更多信息

编辑2查看//第二次编辑评论后的代码

public class MainActivity extends AppCompatActivity {
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//When the activity starts, we look into the shared prefs and get an int of name "ok_clicked" from it.
//0 will be the default value of the int if there is no int stored in sharedPreferences.
prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
int times = prefs.getInt("ok_clicked", 0);
//if the times value is 0, we will open the dialog, otherwise nothing happens
if (times==0){
openDialog();
}
}
//Read This comment First: We will create a Method, which create an alert Dialog.
private void openDialog(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Test").setMessage("Lorem ipsum dolor");
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//When OK button is clicked, an int with value of 1 will be saved in sharedPreferences.
prefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("ok_clicked", 1);
editor.apply();
}
});
dialog.setNegativeButton("No Thanks", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Second Edit: To open another acitivty on No Thanks Button
Intent intent = new Intent(MyActivity.this, HomeActivity.class);
startActivity(intent);
}
});
dialog.show();
}
}

关于java - 如何修改AlertDialog(Android)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35488322/

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