Android Development Interface View Dialog dialog box Example tutorial

Source: Internet
Author: User
Tags stub

User Interface View Dialog dialog box

First, Alertdialog common methods
Create a Alertdialog using the Create () method in Alertdialog.builder

Settitle (); Set Caption To dialog box
SetIcon (); Set icon for dialog box
Setmessage (); Set the message for the dialog box
Setview () to set a custom style for the dialog box
Setitems (); Sets a list to display for the dialog box, which is typically used to display a few commands
Setsinglechoiceitems (); The Setup dialog box displays a single select list
Setmultichoiceitems (); Setup dialog box displays a series of check boxes
Setneutralbutton (); normal button
Setpositivebutton (); Add OK button to dialog box
Setnegativebutton (); Add the Cancel button to the dialog box


Example one:



Figure 1 Effect: This effect is when you press the return button to eject a prompt to ensure that the correct operation, using the common style of the dialog box.

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;
}

Figure 2 Effect: Change the dialog box chart, add three buttons

Dialog Dialog = new Alertdialog.builder (this). SetIcon (
Android. R.drawable.btn_star). Settitle ("Hobby Survey"). Setmessage (
"Do you like Jet Li's movie?" "). 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 movies very much.") ",
Toast.length_long). Show ();
}
}). Setnegativebutton ("Dislike", 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 ();



Figure 3 Effect: The 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 ();



Figure 4 Effect: The information content is a group of radio boxes

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



Figure 5 Effect: Information content is a set of multiple-selection boxes

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 ();



Figure 6 Effect: The information content is a set of simple list items

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



Figure 7 Effect: The information content is a custom layout

1. 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>

2. 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 ();

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.