ProgressDialog and DatePickerDialog and TimePickerDialogPopupWindow prompt dialogs

  • 2021-01-19 22:24:09
  • OfStack

ProgressDialog (precision bar dialog box) :

1. Directly call the static method show() provided by ProgressDialog

2. Create ProgressDialog, set the parameters of the dialog box, and finally create show()


package com.example.test3;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity 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 1 One for updating progress Handler, Because only the main thread can update the interface , So want to use Handler Convey information 
  final Handler hand = new Handler()
  {
    @Override
    public void handleMessage(Message msg) {
      // If you receive a message code here 123
      if(msg.what == 123)
      {
        // Sets the current value of the progress bar 
        pd2.setProgress(progressStart);
      }
      // If the current is greater than or equal to the maximum value of the progress bar , call dismiss() Method closes 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.btn1);
    btn_two = (Button) findViewById(R.id.btn2);
    btn_three = (Button) findViewById(R.id.btn3);
    btn_one.setOnClickListener(this);
    btn_two.setOnClickListener(this);
    btn_three.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    switch (v.getId()){
      case R.id.btn1:
        // In this case, the parameters are in order , context , The title , content , Whether to display progress , Whether it can be closed with the cancel button 
        ProgressDialog.show(MainActivity.this, " Resource loading ", " Resource loading , Please later ...",false,true);
        break;
      case R.id.btn2:
        pd1 = new ProgressDialog(mContext);
        // Set the title one by one , content , Whether to close with the cancel button , Whether to display progress 
        pd1.setTitle(" Software Updates ");
        pd1.setMessage(" The software is being updated , Please later ...");
        pd1.setCancelable(true);
        // Here is the style of the progress bar ,HORIZONTAL It's a horizontal progress bar ,SPINNER It's a circular progress bar 
        pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd1.setIndeterminate(true);
        // call show() Method will be ProgressDialog display 
        pd1.show();
        break;
      case R.id.btn3:
        // Initialize attribute 
        progressStart = 0;
        add = 0;
        // In turn, set 1 Some properties 
        pd2 = new ProgressDialog(MainActivity.this);
        pd2.setMax(MAXVALUE);
        pd2.setTitle(" File in reading ");
        pd2.setMessage(" File loading , Please later ...");
        // This is set so that the progress bar cannot be closed by pressing the cancel button 
        pd2.setCancelable(false);
        pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // This setting is whether to show progress or not , Set to false Is the display oh! 
        pd2.setIndeterminate(false);
        pd2.show();
        // In this case, new 1 A thread , rewrite run() methods ,
        new Thread()
        {
          public void run()
          {
            while(progressStart < MAXVALUE)
            {
              // The algorithm here determines what happens to the progress bar , You can write it as needed 
              progressStart = 2 * usetime() ;
              // Send the information code to handle Let's update the interface 
              hand.sendEmptyMessage(123);
            }
          }
        }.start();
        break;
    }
  }
  // Set up here 1 A time-consuming method :
  private int usetime() {
    add++;
    try{
      Thread.sleep(100);
    }catch (InterruptedException e) {
      e.printStackTrace();
    }
    return add;
  }
}

2.Date/TimePickerDialog only allows the user to select the date and time. The date has no effect on the system time of android system

The constructors for both are very similar: DatePickerDialog(context; DatePickerDialog.OnDateSetListener () listener; Years; Month; Day)
TimePickerDialog(context; TimePickerDialog.OnTimeSetListener () listener; Hours, minutes, whether to use 24-hour system)


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) {
            // So we have to add the month that we got here 1 oh ~
            result += " And you chose "+year+" years "+(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 choose is :"+hourOfDay+" when "+minute+" points ";
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
          }
        }, cale2.get(Calendar.HOUR_OF_DAY), cale2.get(Calendar.MINUTE), true).show();
        break;
    }
  }
}

The last 1 for the display of information UI control - PopupWindow(hover box), if you want to know what he looks like, you can open your mobile phone QQ, long press a list of some items, this time after the popup of a small black dialog box, this is PopupWindow, and AlertDialog dialog box is different, his position can be arbitrary; In addition, AlertDialog is non-blocking, while PopupWindow is blocking

1) Several commonly used construction methods

As you can see from the documentation, there are nine PopupWindow constructors provided to us. Here are just a few of the constructors that are used more frequently in real development:


public PopupWindow (Context context)
public PopupWindow(View contentView, int width, int height)
public PopupWindow(View contentView)
public PopupWindow(View contentView, int width, int height, boolean focusable)

contentView (View, PopupWindow, focusable, PopupWindow, View

2) Some commonly used methods

The following are some of the most commonly used methods. Others can be found in the documentation:

setContentView(View contentView) : Sets the View displayed by PopupWindow
getContentView() : Get the ES61en displayed by ES60en
showAsDropDown(View anchor) : No offset relative to the position of a control (right below left)
showAsDropDown(View anchor, int xoff, int yoff) : Offset from the position of a control
showAtLocation(View parent, int gravity, int x, int y) : You can set offset or no offset relative to the position of the parent control (for example, Gravity.CENTER in the center, Gravity.BOTTOM in the bottom, etc.).
setWidth/setHeight: Set the width and height. You can also specify the width and height in the constructor. In addition to writing the specific value, you can also use WRAP_CONTENT or MATCH_PARENT.

setFocusable(true) : Set the focus, and when PopupWindow pops up, all touch screens and physical keys are handled by PopupWindows. The response to any other event must occur after PopupWindow is gone (except for system-level events such as home). For example, when a PopupWindow appears, press back first to make PopupWindow disappear, and then press activity the second time to exit activity. To exit activity, you have to make PopupWindow disappear first, because pressing back will not disappear under any circumstances, only when PopupWindow is set as the background.

setAnimationStyle(int) : Set animation effects


public class MainActivity extends Activity {
  private Button btn_show;
  private Context mContext;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mContext = MainActivity.this;
    btn_show = (Button) findViewById(R.id.btn_show);
    btn_show.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        initPopWindow(v);
      }
    });
  }
  private void initPopWindow(View v) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.item_popup, null, false);
    Button btn_xixi = (Button) view.findViewById(R.id.btn_xixi);
    Button btn_hehe = (Button) view.findViewById(R.id.btn_hehe);
    //1. structure 1 a PopupWindow , parameters are loaded in turn View Wide, high 
    final PopupWindow popWindow = new PopupWindow(view,
        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
    popWindow.setAnimationStyle(R.anim.anim_pop); // Set up the load animation 
    // These are for clicking not PopupWindow Area, PopupWindow It would disappear if there weren't any below 
    // Code words that you will find when you put PopupWindow It shows up, no matter how many times you press the back button 
    //PopupWindow Does not close and cannot exit the program. Add the following code to solve this problem 
    popWindow.setTouchable(true);
    popWindow.setTouchInterceptor(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        return false;
        //  So here if I return true Words, touch The event will be intercepted 
        //  After the intercept  PopupWindow the onTouchEvent Is not called, such a click on the external area cannot dismiss
      }
    });
    popWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));  // for popWindow Set up the 1 The background is effective 
    // Set up the popupWindow The position shown, in turn, is referenced by the parameters View . x The offset of the axis, y The offset of the shaft 
    popWindow.showAsDropDown(v, 50, 0);
    // Set up the popupWindow The button event in the 
    btn_xixi.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(MainActivity.this, " You clicked Hee Hee ~", Toast.LENGTH_SHORT).show();
      }
    });
    btn_hehe.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(MainActivity.this, " You click hehe ~", Toast.LENGTH_SHORT).show();
        popWindow.dismiss();
      }
    });
  }
}


Related articles: