Android dialog box Summary

Source: Internet
Author: User

Android dialog box Summary
1. What is a dialog box?
A secondary window that contains buttons and various options for specific commands or tasks. The dialog box for "Search" and "replace" is different from the window. It does not have a maximum button, a minimum button, or a large number of shapes and sizes. (The "open file" dialog box can change the size.) dialog box: it is a form of human-computer communication. When you set the dialog box, the computer will execute the corresponding commands. The dialog box contains the single button, check box, and so on.
To me, a dialog box is a small box used for dialog with users. The most common dialog box is pop-up.
2. What is the usage of the dialog box?
The dialog box does not block the main thread. It can interact with users and give users a fresh feeling. A good dialog box does not affect user experience, instead, it will make users like this disturbing feeling.

3. How to use it?

1. System dialog box
AlertDialog
ProgressDialog
DatePickerDialog
TimePickerDialog
2. Custom dialog box
A. Use the setView method of LayoutInflate and Builder to implement
B. inherit from the Dialog class rewrite setContentView implementation
Instance:
Activity_main.xml
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center"
Android: orientation = "vertical">

Android: id = "@ + id/show1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "10dp"
Android: background = "@ drawable/button_background"
Android: text = "dialog1"/>

Android: id = "@ + id/show2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "10dp"
Android: background = "@ drawable/button_background"
Android: text = "dialog2"/>

Android: id = "@ + id/show3"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "10dp"
Android: background = "@ drawable/button_background"
Android: text = "dialog3"/>



Activity_dialog.xml
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">

Android: id = "@ + id/textView1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: text = "title"
Android: textAppearance = "? Android: attr/textAppearanceLarge "/>

Android: id = "@ + id/textView2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: text = "this is a custom Layout AlertDialog"/>

Android: id = "@ + id/textView3"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: text = "youmu has fun"/>

Android: id = "@ + id/textView4"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: text = "layout as needed"
Android: textAppearance = "? Android: attr/textAppearanceMedium "/>

Android: id = "@ + id/button1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/button_background"
Android: text = "OK"/>



MainActivity. java
Public class MainActivity extends Activity {
Private Button show1, show2, show3;
DatePickerDialog m = null;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Show1 = (Button) findViewById (R. id. show1 );
Show2 = (Button) findViewById (R. id. show2 );
Show3 = (Button) findViewById (R. id. show3 );
Show1.setOnClickListener (mClickListener );
Show2.setOnClickListener (mClickListener );
Show3.setOnClickListener (mClickListener );
}

Private View. OnClickListener mClickListener = new OnClickListener (){

@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Int id = arg0.getId ();
Switch (id ){
Case R. id. show1:
ShowDialog1 ();
Break;
Case R. id. show2:
ShowDialog2 ();
Break;
Case R. id. show3:
ShowDialog3 ();
Break;
Default:
Break;
}
}
};

Private void showDialog1 (){
New AlertDialog. Builder (this). setTitle ("dialog 1 ")
. SetMessage ("this is the simplest AlertDialog"). setPositiveButton ("OK", null)
. SetNeutralButton ("what", null). setNegativeButton ("cancel", null)
. Create (). show ();
}

Private void showDialog2 (){
LayoutInflater inflater = LayoutInflater. from (this );
View view = inflater. inflate (R. layout. activity_diater, null );
Final AlertDialog dialog = new AlertDialog. Builder (this). setView (view). create ();
Button OK = (Button) view. findViewById (R. id. button1 );
OK. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Dialog. dismiss ();
}
});
Dialog. show ();
}

Private void showDialog3 (){
New MyDialog (this). show ();
}

Private class MyDialog extends Dialog {

Public MyDialog (Context context ){
Super (context );
// TODO Auto-generated constructor stub
This. setContentView (R. layout. activity_dialog );
This. setTitle ("subject ");
Button B = (Button) findViewById (R. id. button1 );
B. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
Dismiss ();
}
});
}
}
}

Iv. Summary
Note:
1. Do not flood the dialog box
2. Pay attention to dismiss () or remove () when using the custom dialog box ()
3. Another way to define the Dialog box is to set the Theme. Dialog of the Activity.
Summary:
To become a good programmer, you must first become a good designer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.