Basic use of several other common dialog boxes in Android

Introduction to this section:

In the previous section, we studied the parent class of Dialog: AlertDialog, and in this section we will learn the basic usage of several commonly used Dialogs, which are: ProgressDialog (progress bar dialog), DatePickerDialog (date selection dialog) and TimePickerDialog (time selection dialog box)~, not much to say, let’s start this section~


1. Basic use of ProgressDialog (progress bar dialog)

There are two ways we can create a progress bar dialog:

  • 1. Directly call the static method show() provided by ProgressDialog to display
  • 2. Create a ProgressDialog, then set the parameters of the dialog box, and finally show() it out

Code example :

Running effect diagram :

Key implementation code :

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_one;
    private Button btn_two;
    private Button btn_three;
    private ProgressDialog pd1 = null;
    private ProgressDialog pd2 = null;
    private final static int MAXVALUE = 100;
    private int progressStart = 0;
    private int add = 0;
    private Context mContext = null;


    //Define a Handler for updating the progress, because the interface can only be updated by the main thread, so use the Handler to pass information
    final Handler hand = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            //Here, if the received information code is 123
            if(msg.what == 123)
            {
                //Set the current value of the progress bar
                pd2.setProgress(progressStart);
            }
            //If the current value is greater than or equal to the maximum value of the progress bar, call the dismiss() method to close the dialog box
            if(progressStart >= MAXVALUE)
            {
                pd2.dismiss();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        bindViews();
    }

    private void bindViews() {
        btn_one = (Button) findViewById(R.id.btn_one);
        btn_two = (Button) findViewById(R.id.btn_two);
        btn_three = (Button) findViewById(R.id.btn_three);
        btn_one.setOnClickListener(this);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_one:
                //The parameters here are, context, title, content, whether to display progress, whether to close with the cancel button
                ProgressDialog.show(MainActivity.this, "Resource loading", "Resource loading, please wait...", false, true);
                break;
            case R.id.btn_two:
                pd1 = new ProgressDialog(mContext);
                //Set the title, content in turn, whether to close with the cancel button, whether to display the progress
                pd1.setTitle("Software update");
                pd1.setMessage("The software is being updated, please wait...");
                pd1.setCancelable(true);
                //Here is the style of setting the progress bar, HORIZONTAL is the horizontal progress bar, SPINNER is the circular progress bar
                pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd1.setIndeterminate(true);
                //Call the show() method to display the ProgressDialog
                pd1.show();
                break;
            case R.id.btn_three:
                //Initialize properties
                progressStart = 0;
                add = 0;
                // Set some properties in turn
                pd2 = new ProgressDialog(MainActivity.this);
                pd2.setMax(MAXVALUE);
                pd2.setTitle("reading file");
                pd2.setMessage("File loading, please wait...");
                //It is set here that the progress bar cannot be closed by pressing the cancel button
                pd2.setCancelable(false);
                pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //The setting here is whether to display the progress, set it to false to display it!
                pd2.setIndeterminate(false);
                pd2.show();
                //Here, create a new thread and rewrite the run() method,
                new Thread()
                {
                    public void run()
                    {
                        while(progressStart < MAXVALUE)
                        {
                            //The algorithm here is to determine the change of the progress bar, which can be written as needed
                            progressStart = 2 * usetime() ;
                            //Send the information code to the handle to update the interface
                            hand.sendEmptyMessage(123);
                        }
                    }
                }.start();
                break;
        }
    }

    //Set up a time-consuming method here:
    private int usetime() {
        add++;
        try{
            Thread.sleep(100);
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        return add;
    }
}

The code is relatively simple, and we have already learned about Progress, so I won’t be too long-winded here~


2. DatePickerDialog (date selection dialog box) and TimePickerDialog (time selection dialog box)

Let me explain one thing first: Date/TimePickerDialog is only for users to choose the date and time. It has no effect on the system time and date of the android system. Google has not announced the API for setting the system date and time for the time being. If you want to set it in the app, you need to recompile it The source code of the android system is very troublesome!

The constructors of the two are very similar:  DatePickerDialog (context; DatePickerDialog.OnDateSetListener() listener; year; month; day)
TimePickerDialog (context; TimePickerDialog.OnTimeSetListener() listener; hour, minute, whether to use 24-hour format)

Code example :

Running effect diagram :

Key implementation code :

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_date;
    private Button btn_time;
    private String result = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();
    }

    private void bindViews() {
        btn_date = (Button) findViewById(R.id.btn_date);
        btn_time = (Button) findViewById(R.id.btn_time);

        btn_date.setOnClickListener(this);
        btn_time.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        result = "";
        switch (v.getId()){
            case R.id.btn_date:
                Calendar cale1 = Calendar.getInstance();
                new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                                          int dayOfMonth) {
                        //The month obtained here needs to add 1~
                        result += "You choose "+year+"year"+(monthOfYear+1)+"month"+dayOfMonth+"day";
                        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                    }
                }
                        ,cale1.get(Calendar.YEAR)
                        ,cale1.get(Calendar.MONTH)
                        ,cale1.get(Calendar.DAY_OF_MONTH)).show();
                break;
            case R.id.btn_time:
                Calendar cale2 = Calendar.getInstance();
                new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        result = "";
                        result += "The time you selected is:"+hourOfDay+"hour"+minute+"minute";
                        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                    }
                }, cale2.get(Calendar.HOUR_OF_DAY), cale2.get(Calendar.MINUTE), true).show();
                break;
        }
    }
}

Guess you like

Origin blog.csdn.net/leyang0910/article/details/131140829