繁体   English   中英

如何修改AlertDialog(Android)?

[英]How to modify AlertDialog (Android)?

我是Java以及Android Studio的初学者,因此,我的问题是:我为活动创建了一个警报对话框窗口,其中肯定按钮为“确定”,否定按钮为“不谢谢”。 如下面的代码所示。

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循环中,并在用户单击“确定”时增加其值,这样,如果用户单击“确定”,则该循环只能执行一次,但是每当我打开活动警报框时该循环都没有用尽管单击“确定”仍显示。 所以,现在我想做的事情是:

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

  2. 如果用户单击“不,谢谢”按钮,我想带他去家庭活动。 因此,我应该如何使用带有“不,谢谢”按钮的意图?

谢谢。

您需要使用SharedPreferenes持久保存数据,局部变量将无济于事。 像这样的东西:

编辑根据您的要求,我添加了一个示例活动类来显示整个过程。 有关更多信息,请参见之间的评论

编辑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();
            }
        }

暂无
暂无

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

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