简体   繁体   中英

How to modify AlertDialog (Android)?

I'm a beginner for Java as well as for Android Studio so, here my problem is: I had created a alert dialog window for an activity with positive button being "OK" and negative button being "No thanks". As shown in the code below.

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();

                    }
                });
 }

It was going fine but now the catch is I want it to be displayed only once if the user clicks the "OK" button and don't want to show it again if the user clicked "OK". I had created a variable times in my class and initialised it to zero as shown below.

public class rootingRooting extends AppCompatActivity {

int Times=0;

and put the complete AlertDialog in the if loop and incremented it's value when the user clicked "OK" so that the loop may execute only once if the user clicked "OK", but it is of no use whenever I open the activity the alert box is being displayed inspite of clicking "OK". So, now the things i want to do happen is:

  1. The alert box should not be displayed if the user once clicked "OK".

  2. If the user clicked the "no Thanks" button, I want to take him to the home activity. So, how should I use the intent with the "no thanks" button?

Thank you.

You need to use SharedPreferenes to save data persistently, local variables will not help. something like this:

EDIT As per your request, I have added a sample activity class to show the whole process. See the comments in between for more info

EDIT 2 See the code after //Second Edit comment

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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