Android Dialog Daquan

Source: Internet
Author: User

1. This effect is a prompt that pops up when you press the Back button to ensure error-correct operation, using a common dialog box style.


Code:

Create a dialog box Method dialog ()
protected void dialog () {
Alertdialog.builder builder = new Builder (main.this);
Builder.setmessage ("Confirm exit?") ");
Builder.settitle ("hint");
Builder.setpositivebutton ("Confirm", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Dialog.dismiss ();
Main.this.finish ();
}
});
Builder.setnegativebutton ("Cancel", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Dialog.dismiss ();
}
});
Builder.create (). Show ();
}

This method is called in the onkeydown (int keycode, KeyEvent event) method
public boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back && event.getrepeatcount () = = 0) {
Dialog ();
}
return false;
}

2. Changed the chart of the dialog box, added three buttons

Dialog Dialog = new Alertdialog.builder (this). SetIcon (
Android. R.drawable.btn_star). Settitle ("Survey of Preferences"). Setmessage (
"Do you like Jet Li's movies?" "). Setpositivebutton (" Very like ",
New Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Toast.maketext (Main.this, "I like his movie very much. ",
Toast.length_long). Show ();
}
}). Setnegativebutton ("Don't like", New Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Toast.maketext (Main.this, "I don't like his movies. ", Toast.length_long)
. Show ();
}
}). Setneutralbutton ("General", new Onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
TODO auto-generated Method Stub
Toast.maketext (Main.this, "I don't like it. ", Toast.length_long)
. Show ();
}
}). Create ();
Dialog.show ();

3. Information content is a simple view type

New Alertdialog.builder (this). Settitle ("Please enter"). SetIcon (
Android. R.drawable.ic_dialog_info). Setview (
New EditText (This)). Setpositivebutton ("OK", NULL)
. Setnegativebutton ("Cancel", null). Show ();

4. Information content is a set of radio boxes

New Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (
New string[] {"Item1", "Item2"}, NULL, NULL)
. Setpositivebutton ("OK", NULL)
. Setnegativebutton ("Cancel", null). Show ();

5. Information content is a set of multi-marquee

New Alertdialog.builder (this). Settitle ("Radio Box"). SetIcon (
Android. R.drawable.ic_dialog_info). Setsinglechoiceitems (
New string[] {"Item1", "Item2"}, 0,
New Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
Dialog.dismiss ();
}
}). Setnegativebutton ("Cancel", null). Show ();

6. Information content is a set of simple list items

New Alertdialog.builder (this). Settitle ("list box"). Setitems (
New string[] {"Item1", "Item2"}, NULL). Setnegativebutton (
OK, null). Show ();

7. Information content is a custom layout

Layout file
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:layout_height= "Wrap_content" android:layout_width= "Wrap_content"
Android:background= "#ffffffff" android:orientation= "Horizontal"
Android:id= "@+id/dialog" >
<textview android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
Android:id= "@+id/tvname" android:text= "Name:"/>
<edittext android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content" android:id= "@+id/etname" android:minwidth= "100dip"/>
</LinearLayout>
Calling code
Layoutinflater inflater = Getlayoutinflater ();
View layout = Inflater.inflate (R.layout.dialog,
(ViewGroup) Findviewbyid (R.id.dialog));
New Alertdialog.builder (this). Settitle ("Custom Layout"). Setview (Layout)
. Setpositivebutton ("OK", NULL)
. Setnegativebutton ("Cancel", null). Show ();

Android Dialog Daquan

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.