Alertdialog. Builder dialog box class usage (2)

Source: Internet
Author: User

Return information from the dialog box:
Now, it's time to get the user input value from the dialog box and return it to the activity of the main call. However, the dialog class does not provide a method to directly return these values... However, we can use the listener class we created:

Public interface mydialoglistener {
Public void onokclick (string name );
Public void oncancelclick ();
}

The constructor of the dialog box class also needs to make a small modification:

Public mydialog (context, mydialoglistener listener ){
Super (context );
This. Listener = listener;
}

Then, you have to provide a listener implementation object that has implemented the mydialoglistener interface when creating this dialog box.
Then, we need to output this value in The onclick method of the dialog box:

Public void onclick (view ){
Switch (view. GETID ()){
Case R. Id. okbutton:
Listener. onokclick (nameedittext. gettext (). tostring ());
Dismiss ();
Break;
Case R. Id. cancelbutton:
Cancel ();
Break;
}
}

Use alertdialog:
The alertdialog class is a subclass of the dialog class. It provides three buttons and a text message by default. These buttons can be displayed or hidden as needed. The following code creates an alertdialog dialog box. A question and alternative yes/no answers are displayed in the dialog box:

Alertdialog dialog = new alertdialog. Builder (context). Create ();
Dialog. setmessage ("do you play cricket ?");
Dialog. setbutton ("yes", myonclicklistener );
Dialog. setbutton2 ("no", myonclicklistener );
Dialog. Show ();

The code of the event listener myonclicklistener can be similar to the following:

Public void onclick (dialoginterface dialog, int I ){
Switch (I ){
Case alertdialog. button1:

Break;
Case alertdialog. button2:

Break;
}
}

Alertdialog. Builder:
The alertdialog class has an internal class,The name is 'builder'. The Builder class provides the ability to add multiple or single-choice lists for the dialog box.And add the event processing function for these lists. In addition, the builder class calls the three buttons in the alertdialog dialog box positivebutton, neutralbutton, and negativebutton according to their respective positions.
The following code is an example of a multiple-choice list:

New alertdialog. Builder (context)
. Seticon (R. drawable. Icon)
. Settitle (R. String. alert_dialog_multi_choice)
. Setmultichoiceitems (
R. array. select_dialog_items,
New Boolean [] {false, true, false, true, false },
New dialoginterface. onmultichoiceclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton, Boolean ischecked ){

}
}). Setpositivebutton (R. String. alert_dialog_ OK, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){

}
}). Setnegativebutton (R. String. alert_dialog_cancel, new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int whichbutton ){

}
}
). Create ();

 

Activity hosting dialog box:
Android also provides a shortcut for creating a dialog box. In the activity, you can use showdialog (), oncreatedialog (), onpreparedialog (), dismissdialog (), and removedialog () to create and manage the dialog box.
The oncreatedialog method of the activity is called when a dialog box is created and displayed, for example:

@ Override
Protected dialog oncreatedialog (int id ){
Return new alertdialog. Builder (this). setmessage ("how are you ?"). Setpositivebutton (
"Fine ",
New dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){

}
}). Setnegativebutton ("not so good", new dialoginterface. onclicklistener (){
Public void onclick (dialoginterface dialog, int which ){

}
}
). Create ();
}

You can create multiple dialogs at the same time, differentiate them by setting ID parameters for them, and then display them using the showdialog (ID) method. The oncreatedialog method is called only when the showdialog method is called for the first time. In subsequent showdialog () calls, the dialog box is not created, it directly displays the previously created dialog boxes.

If you want to update the content of the dialog box, you only need to do the corresponding work in onpreparedialog (). This method will be called before the display of the dialog box.
The dismissdialog () method is used to close the dialog box. The removedialog () method is used to remove the dialog box from activity hosting (if showdialog is re-called for the removed dialog box, the dialog box is created again ).

Use Dialog topic:
Another simple way to display a dialog box is to make the activity work in dialog mode (pretend ?), This kind of activity is called a floating activity. This activity can be implemented by configuring its topic. We can perform the following configuration in androidmanifest. xml:

<Activity Android: Name = ". dialogactivity" Android: Label = "@ string/activity_dialog" Android: theme = "@ Android: style/theme. Dialog">
...
</Activity>

In this way, the activity applies the theme. Dialog 'topic and looks like a dialog box.

Original article: http://www.oschina.net/discuss/thread/1567

Create dialog box method Dialog ()

Protected void Dialog (){
Alertdialog. Builder = new Builder (main. This );
Builder. setmessage ("are you sure you want to exit? ");

Builder. settitle ("prompt ");

Builder. setpositivebutton ("OK", 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 ();
}

 

Call this method 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:Changed the chart in the dialog box and added three buttons.

Dialog dialog = new alertdialog. Builder (this). seticon (
Android. R. drawable. btn_star). settitle ("Preferences"). setmessage (
"Do you like Jet Li's film? "). Setpositivebutton (" very fond ",
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, "Do not like it or not. ", Toast. length_long)
. Show ();
}
}). Create ();

Dialog. Show ();

 

Figure 3 effect:Information Content is a simple view type

New alertdialog. Builder (this). settitle ("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 single queues

New alertdialog. Builder (this). settitle ("check box"). setmultichoiceitems (
New String [] {"Item1", "item2"}, null, null)
. Setpositivebutton ("OK", null)
. Setnegativebutton ("cancel", null). Show ();

 

Figure 5 effect:The information content is a set of multiple selection boxes

New alertdialog. Builder (this). settitle ("single button"). 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: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 ();

 

Figure 7 effect: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. Call Code

Layoutinflater Inflater = getlayoutinflater ();
View layout = Inflater. Inflate (R. layout. diater,
(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.