Android learning-various dialogs

Source: Internet
Author: User

In android programming, the dialog box is very useful. However, sometimes the requirements for the dialog box are different. Now, we will summarize the types of the dialog box and the handling situation of the event.

1. Simple dialog box


[Java]
Public void showDialog (){
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("title ")
. SetMessage ("message ")
. SetPositiveButton ("good", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
})
. SetNegativeButton ("Not good", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
}). Create ();
Dialog. show ();
}

Public void showDialog (){
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("title ")
. SetMessage ("message ")
. SetPositiveButton ("good", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
})
. SetNegativeButton ("Not good", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
}). Create ();
Dialog. show ();
}

 

2. Dialog Box with edit box

[Java]
Public void showDialog (){
EditText et = new EditText (this); // separate the data to facilitate user input.
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("enter ")
. SetIcon (android. R. drawable. ic_dialog_info)
. SetView (et)
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}

})
. SetNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}})
. Create ();
Dialog. show ();
}

Public void showDialog (){
EditText et = new EditText (this); // separate the data to facilitate user input.
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("enter ")
. SetIcon (android. R. drawable. ic_dialog_info)
. SetView (et)
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}

})
. SetNegativeButton ("cancel", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}})
. Create ();
Dialog. show ();
}

 

3. Dialog Box with single button

[Java]
Public void showDialog (){
Final String [] str = new String [] {"first part", "second part", "second part", "third part", "fourth part", "fifth part ", "Part 6 "};
// Separate columns for easy data retrieval
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("single region, please select ")
. SetSingleChoiceItems (str, 3, new DialogInterface. OnClickListener (){
// 3 indicates the single sequence whose index is 3 selected by default.
@ Override
Public void onClick (DialogInterface dialog, int which ){
Log. e ("DIYDialogActivity", "showDialog-> you select" + str [which]);
}
})
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
})
. Create ();
Dialog. show ();
}

Public void showDialog (){
Final String [] str = new String [] {"first part", "second part", "second part", "third part", "fourth part", "fifth part ", "Part 6 "};
// Separate columns for easy data retrieval
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("single region, please select ")
. SetSingleChoiceItems (str, 3, new DialogInterface. OnClickListener (){
// 3 indicates the single sequence whose index is 3 selected by default.
@ Override
Public void onClick (DialogInterface dialog, int which ){
Log. e ("DIYDialogActivity", "showDialog-> you select" + str [which]);
}
})
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
})
. Create ();
Dialog. show ();
}

 

4. Dialog Box with multiple selection boxes

[Java]
Public void showDialog (){
Final String [] str = new String [] {"first part", "second part", "second part", "third part", "fourth part", "fifth part ", "Part 6"}; // column it separately to retrieve data.
Boolean [] bstr = new boolean [] {true, false, false };
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("select multiple boxes, please select ")
. SetMultiChoiceItems (str, bstr, new DialogInterface. OnMultiChoiceClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which, boolean isChecked ){
Log. e ("DIYDialogActivity", "showDialog->" +
"You select" + str [which] + "vaule =" + new Boolean (isChecked). toString ());
}
})
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
}). Create ();
Dialog. show ();
}

Public void showDialog (){
Final String [] str = new String [] {"first part", "second part", "second part", "third part", "fourth part", "fifth part ", "Part 6"}; // column it separately to retrieve data.
Boolean [] bstr = new boolean [] {true, false, false };
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("select multiple boxes, please select ")
. SetMultiChoiceItems (str, bstr, new DialogInterface. OnMultiChoiceClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which, boolean isChecked ){
Log. e ("DIYDialogActivity", "showDialog->" +
"You select" + str [which] + "vaule =" + new Boolean (isChecked). toString ());
}
})
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
}). Create ();
Dialog. show ();
}

 

5. List-included dialog box

[Java]
Public void showDialog (){
Final String [] str = new String [] {"first part", "second part", "second part", "third part", "fourth part", "fifth part ", "Part 6 "};
// Separate columns for easy data retrieval
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("list box for selection ")
. SetItems (str, new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Log. e ("DIYDialogActivity", "showDialog ------------------- you select" + which );
}
})
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
})
. Create ();
Dialog. show ();
}

Public void showDialog (){
Final String [] str = new String [] {"first part", "second part", "second part", "third part", "fourth part", "fifth part ", "Part 6 "};
// Separate columns for easy data retrieval
Dialog dialog = new AlertDialog. Builder (this)
. SetTitle ("list box for selection ")
. SetItems (str, new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){
Log. e ("DIYDialogActivity", "showDialog ------------------- you select" + which );
}
})
. SetPositiveButton ("OK", new DialogInterface. OnClickListener (){
@ Override
Public void onClick (DialogInterface dialog, int which ){

}
})
. Create ();
Dialog. show ();
}
6. Custom dialog box


[Java]
Public void showDialog (){
LayoutInflater inflater = getLayoutInflater ();
View layout = inflater. inflate (R. layout. dialog_showmsg, (ViewGroup) findViewById (R. id. view_dialog ));
// You can specify the background colors of the dialog box.

Dialog dialog = new AlertDialog. Builder (this)
. SetView (layout)
. Create ();

// Set the location where the dialog box appears, with the help of the window object
Window win = dialog. getWindow ();
// Win. setGravity (Gravity. CENTER );
// Win. setGravity (Gravity. BOTTOM );
// Win. setGravity (Gravity. TOP );
Win. clearFlags (WindowManager. LayoutParams. FLAG_DIM_BEHIND); // In the pop-up dialog box, the bottom form is not dimmed.

WindowManager. LayoutParams lp = win. getAttributes ();
Lp. x =-200; // when x = 0, y = 0, the display position is the center of the screen.
Lp. y = 0;
Lp. alpha = 0.6f; // transparency of the dialog box
Win. setAttributes (lp );

Button but = (Button) layout. findViewById (R. id. but_positive );
But. requestFocus ();
Dialog. show ();
}

Public void showDialog (){
LayoutInflater inflater = getLayoutInflater ();
View layout = inflater. inflate (R. layout. dialog_showmsg, (ViewGroup) findViewById (R. id. view_dialog ));
// You can specify the background colors of the dialog box.

Dialog dialog = new AlertDialog. Builder (this)
. SetView (layout)
. Create ();

// Set the location where the dialog box appears, with the help of the window object
Window win = dialog. getWindow ();
// Win. setGravity (Gravity. CENTER );
// Win. setGravity (Gravity. BOTTOM );
// Win. setGravity (Gravity. TOP );
Win. clearFlags (WindowManager. LayoutParams. FLAG_DIM_BEHIND); // In the pop-up dialog box, the bottom form is not dimmed.

WindowManager. LayoutParams lp = win. getAttributes ();
Lp. x =-200; // when x = 0, y = 0, the display position is the center of the screen.
Lp. y = 0;
Lp. alpha = 0.6f; // transparency of the dialog box
Win. setAttributes (lp );

Button but = (Button) layout. findViewById (R. id. but_positive );
But. requestFocus ();
Dialog. show ();
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Related Article

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.