Android Dialog dialog box detailed _android

Source: Internet
Author: User
Tags stub

No more nonsense to say, directly to everyone paste code.

Layout file xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:o" rientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen Activity_horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen Activity_vertical_margin "tools:context=". Dialogactivity "> <button android:id=" @+id/plaindialog "android:layout_width=" Match_parent "Android:layout_" height= "Wrap_content" android:text= "Ordinary Dialog"/> <button android:id= "@+id/plaindialogevent" Android:layout_ Width= "Match_parent" android:layout_height= "wrap_content" android:text= "dialog button event Focus"/> <button = "@+id/inputdialog" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "Please enter the box"/ > <button android:id= "@+id/listdialog" android:layout_width= "match_parent"Android:layout_height=" wrap_content "android:text=" List dialog/> <button android:id= "@+id/radiodialog" Android: Layout_width= "Match_parent" android:layout_height= "Wrap_content" android:text= "Radio dialog box"/> <Button-android:id= " @+id/checkboxdialog "android:layout_width=" match_parent "android:layout_height=" wrap_content "android:text=" Multi-selection dialog box "/> <button android:id=" @+id/diydialog "android:layout_width=" match_parent "android:layout_height=" Wrap_  Content "android:text=" Custom Layout dialog box/> </LinearLayout>

Activity file:

The ordinary dialog:

private void Plaindialogdemo () {
button plainbtn = (button) Findviewbyid (r.id.plaindialog);
Plainbtn.setonclicklistener (New Onclicklistener () {public
void OnClick (View v) {
new Alertdialog.builder ( dialogactivity.this).
settitle ("delete").
setmessage ("Determine delete specified data")
. Setpositivebutton ("OK",
new Dialoginterface.onclicklistener () {
@Override public
void OnClick (Dialoginterface dialog,
int which) {
Toast.maketext (Getapplicationcontext (),
"OK", Toast.length_short)
. Show ()
. Setnegativebutton ("Cancel",
new Dialoginterface.onclicklistener () {
@Override public
void OnClick ( Dialoginterface dialog,
int which) {
}
}). Setcancelable (False). Show ();
}
}

The effect is as follows:

Enter the dialog for the text box:

private void Inputdialog () {
button inputbtn = (button) Findviewbyid (r.id.inputdialog);
Inputbtn.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
//TODO auto-generated method Stub
final edittext et = new EditText (dialogactivity.this);
New Alertdialog.builder (Dialogactivity.this)
. Settitle ("Please enter a number").
setview (ET)
. Setpositivebutton (" OK,
new Dialoginterface.onclicklistener () {
@Override public
void OnClick (Dialoginterface dialog,
int which) {
//TODO auto-generated method Stub
Toast.maketext (Getapplicationcontext (),
et.gettext ()
, Toast.length_short). Show ();
}
}. Setnegativebutton ("Cancel", null)
. Setcancelable (False). Show ();}}

The effect is as follows:


List dialog:

private void Listdialogdemo () {
button listbtn = (button) Findviewbyid (r.id.listdialog);
Listbtn.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
final string[] names = {"Ronaldo", "J Rom", "H Rom"};
New Alertdialog.builder (Dialogactivity.this). Settitle (List dialog box)
. Setitems (Names, new Dialoginterface.onclicklistener () {
@Override public
void OnClick (Dialoginterface dialog,
int which) {
Toast.maketext (Dialogactivity.this,
Names[which], toast.length_short). Show
();
Setnegativebutton ("Cancel", null). Show ();
}
);

The effect is as follows:


Radio dialog:

private void Radiodialogdemo () {
button RadioButton = (button) Findviewbyid (r.id.radiodialog);
Radiobutton.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
final String[] names = {"Ronaldo", "J Rom", "H Rom"};
New Alertdialog.builder (Dialogactivity.this)
. Settitle (List dialog box)
. Setsinglechoiceitems (names,
new Dialoginterface.onclicklistener () {
@Override public
void OnClick (Dialoginterface dialog,
int which) {
selectename = Names[which];
}
)
. Setpositivebutton (ok
), new Dialoginterface.onclicklistener () {
@Override public
void OnClick ( Dialoginterface dialog,
int which) {
toast.maketext (dialogactivity.this,
selectename, Toast.length_ Short)
. Show ();
}
}. Setnegativebutton ("Cancel", null). Show ();
}
);

The effect is as follows:


Multiple selection Dialog:

private void Checkdialogdemo () {
button checkbtn = (button) Findviewbyid (r.id.checkboxdialog);
Checkbtn.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
final string[ ] names = {"Ronaldo", "J Rom", "H Rom"};
Final boolean[] selected = new boolean[] {true, false, true};
New Alertdialog.builder (Dialogactivity.this)
. Setmultichoiceitems (
names,
selected,
new Dialoginterface.onmultichoiceclicklistener () {
@Override public
void OnClick (Dialoginterface dialog,
int which, Boolean ischecked) {
}
})
. Setpositivebutton (OK),
new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog,
int which) {
StringBuilder sb = new StringBuilder (
"you chose:"); for
(int i =; i < names.length; i++) {
if (selected[i)) {
sb.append (names[i]);
}
Toast.maketext (Dialogactivity.this,
sb.tostring (),). Show ();
}
}. Setnegativebutton ("Cancel", null). Show ();
}
);

The effect is as follows:


Custom dialog:

private void Customdialogdemo () {
final Alertdialog dlg = new Alertdialog.builder (this). Create ();
Dlg.show ();
window window = Dlg.getwindow ();
Window.setcontentview (r.layout.diylayout);
ImageButton OK = (ImageButton) Window.findviewbyid (R.id.btnok);
Ok.setonclicklistener (New View.onclicklistener () {
@Override public
void OnClick (View v) {
Toast.maketext (Getapplicationcontext (), "Off",
Toast.length_short). Show ();
Dlg.dismiss ();}}
);

Custom layout:

 <?xml version=". "Encoding=" utf-"?> <relativelayout, xmlns:android=" http:// Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_parent " > <imageview android:id= "@+id/dialogimg" android:layout_width= wrap_content "android:layout_height=" Wrap_ Content "Android:layout_centerinparent=" true "android:src=" @drawable/dialog_bg "/> <textview android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@id/dialogimg" android:layout _aligntop= "@id/dialogimg" android:layout_marginleft= "DP" android:layout_margintop= "DP" android:text= "custom Dialog"/ > <imagebutton android:id= "@+id/btnok" Android:layout_width= "DP" android:layout_height= "DP" Android:layout_ alignright= "@id/dialogimg" android:layout_aligntop= "@id/dialogimg" android:layout_marginright= "DP" Android:layout _margintop= "DP" android:background= "@drawable/close_dialog"/> </RelativeLayout> 

Effects such as:

About Android Dialog dialog box to explain the small series to introduce so many people, I hope to help you!

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.