2.6.0 Basic Usage of Other Common Dialogs
Category Android Basic Tutorial
Introduction to This Section:
>
In the previous section, we studied the parent class of Dialog: AlertDialog. In this section, let's learn about the basic usage of several commonly used Dialogs, which are: ProgressDialog (progress bar dialog), DatePickerDialog (date selection dialog), and TimePickerDialog (time selection dialog). Without further ado, let's start the content of this section~
1. Basic Usage of ProgressDialog (Progress Bar Dialog)
>
There are two ways to create a progress bar dialog:
1. Directly call the static method show() provided by ProgressDialog to display.
2. Create a ProgressDialog, set the dialog parameters, and finally show() it.
Code Example :
Running Effect Picture :
Key Implementation Code :
MainActivity.java :
```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 progress, as only the main thread can update the interface, so a Handler is needed to pass information
final Handler hand = new Handler()
{
@Override
public void handleMessage(Message msg) {
// Here, if the received message 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
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:
// Here, the parameters in order are: context, title, content, whether to show progress, whether it can be closed with the cancel button
ProgressDialog.show(MainActivity.this, "Loading Resources", "Please wait while resources are loading...", false, true);
break;
case R.id.btn_two:
pd1 = new ProgressDialog(mContext);
// Set title, content, whether it can be closed with the cancel button, and whether to show progress in order
pd1.setTitle("Software Updating");
pd1.setMessage("The software is updating, please wait...");
pd1.setCancelable(true);
// Here, set the style of the progress bar, HORIZONTAL is a horizontal progress bar, SPINNER is a 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 order
pd2 = new ProgressDialog(MainActivity.this);
pd2.setMax(MAXVALUE);
pd2.setTitle("Reading File");
pd2.setMessage("Please wait while the file is loading...");
// Here, set it so that the progress bar cannot be closed by pressing the cancel button
pd2.setCancelable(false);
pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// Here, set whether to show progress, set to false to display it!
pd2.setIndeterminate(false);
pd2.show();
// Here, create a new thread, override the run() method,
new Thread()
{
public void run()
{
while(progressStart < MAXVALUE)
{
// The algorithm here determines the change of the progress bar, which can be written as needed
progressStart = 2 * usetime() ;
// Send the message code to hand to update the interface
hand.sendEmptyMessage(123);
}
}
}.start();
break;
}
}
// Here, set a time-consuming method:
private int usetime() {
add++;
try{
Thread.sleep(100
The code is quite straightforward, so I won't explain it further.
3. Code Download:
Summary of This Section:
>
Well, this section introduces three commonly used Dialogs. Compared to the 4.x versions, these native controls in 5.0 are obviously much more visually appealing. That's all for now, thank you.
-1.0 Android Basic Tutorial Introduction
-1.0.1 Latest Android Basic Tutorial Catalog for 2015
-1.1 Background and System Architecture Analysis
-1.2 Development Environment Setup
-1.2.1 Developing Android APP with Eclipse + ADT + SDK
-1.2.2 Developing Android APP with Android Studio
-1.3 Solving SDK Update Issues
-1.4 Genymotion Emulator Installation
-1.5.1 Git Tutorial on Basic Operations of Local Repositories
-1.5.2 Git: Using GitHub to Set Up a Remote Repository
-1.6 How to Play with 9 (Nine Sister) Images
-1.7 Interface Prototype Design
-1.8 Project Related Analysis (Various Files, Resource Access)
-1.9 Android Program Signing and Packaging
-1.11 Decompiling APK to Retrieve Code & Resources
-2.1 The Concept of View and ViewGroup
-2.2.1 LinearLayout (Linear Layout)
-2.2.2 RelativeLayout (Relative Layout)
-2.2.3 TableLayout (Table Layout)
-2.2.4 FrameLayout (Frame Layout)
-2.2.5 GridLayout (Grid Layout)
-2.2.6 AbsoluteLayout (Absolute Layout)
-2.3.1 TextView (Text Box) Detailed Explanation
-2.3.2 EditText (Input Box) Detailed Explanation
-2.3.3 Button (Button) and ImageButton (Image Button)
-2.3.5 RadioButton (Radio Button) & Checkbox (Checkbox)
-2.3.6 ToggleButton (Toggle Button) and Switch (Switch)
-2.3.7 ProgressBar (Progress Bar)
-2.3.9 RatingBar (Star Rating Bar)
-2.4.1 ScrollView (Scroll View)
-2.4.2 Date & Time Components (Part 1)
-2.4.3 Date & Time Components (Part 2)
-2.4.5 ListView Simple and Practical Use
-2.4.6 BaseAdapter Optimization
-2.4.8 Solving Checkbox Misalignment Issues in ListView
-2.4.9 Data Update Issues in ListView
-[2.5.0 Building a Reusable Custom BaseAdapter]
2.6.0 Basic Usage of Other Common Dialog Boxes
2.6.4 Simple Usage of DrawerLayout (Official Side-Slide Menu)
3.3 A Brief Analysis of the Handler Message Passing Mechanism
3.6 Responding to System Setting Events (Configuration Class)
4.4.2 Further Exploration of ContentProvider - Document Provider
5.2.1 Detailed Explanation of Fragment - Implementing a Bottom Navigation Bar (Method 1)
5.2.2 Detailed Explanation of Fragment - Implementing a Bottom Navigation Bar (Method 2)
5.2.3 Detailed Explanation of Fragment - Implementing a Bottom Navigation Bar (Method 3)
5.2.4 Detailed Explanation of Fragment - Bottom Navigation Bar + ViewPager Page Swiping
6.2 Data Storage and Access - SharedPreferences to Save User Preferences
7.1.1 What to Learn in Android Network Programming and Study of the Http Protocol
[7.3.3 Android
8.3.14 Paint Enum/Constant Values and ShadowLayer Shadow Effects
8.3.17 Detailed Explanation of Canvas API (Part 2) - Collection of Clipping Methods
8.3.18 Detailed Explanation of Canvas API (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - First Glance
8.4.4 Android Animation Collection - Property Animation - Another Look
11.0 "The Latest 2015 Android Basic Tutorial" Concludes with Fireworks~
12.2 DrySister Girl Viewing App (First Edition) - 2. Parsing Backend Data
12.4 DrySister Girl Viewing App (First Edition) - 4. Adding Data Caching (Integrate SQLite)