Android untitled dialog and dialog style forms

Source: Internet
Author: User
After the normal method of a form without a title dialog or dialog style in Android pops up alert, a small part of the height will be displayed when the title is canceled. This will be better: first define a style Res/values/styles. xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "fullheightdialog"
Parent = "Android: style/theme. Dialog">
<Item name = "Android: windownotitle"> true </item>
</Style>
</Resources>

When alert is displayed, modify it a little:

Dialog dialog = new dialog (this, R. style. fullheightdialog );
Dialog. setcontentview (R. layout. article_comment );
Dialog. Show ();

You can pop up a form with a similar effect as alert.
First, modify the properties of the form:

Modify the androidmanifest. xml file:

<Activity Android: Name = ". test"
Android: theme = "@ Android: style/theme. Dialog">
</Activity>

This is a declaration style. Here are some parameters for oncreate after opening activity:

Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Requestwindowfeature (window. feature_no_title );
Getwindow (). setflags (windowmanager. layoutparams. flag_blur_behind,
Windowmanager. layoutparams. flag_blur_behind );

This. setcontentview (R. layout. article_comment );
}

In this case, the background is blurred.

In the previous learning process, we know that the most common screen display development in Android applications is based on activity. However, in many cases, you need to display a dialog box or floating form to complete some simple tasks, such as asking the user to enter some content or asking the user to confirm some information.

In Android, you can create a dialog box in two ways:
1. Use the dialog class or its subclass (such as alertdialog)
2. Use the theme of the activity dialog box

Use the dialog class:
Let's first look at how to create a dialog box using the dialog class. First, we need to define a subclass that inherits the dialog class:

Class mydialog extends dialog {
Public mydialog (context ){
Super (context );
}
}

Then, define a layout file for the content of this dialog box, for example:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Android: Id = "@ + ID/widget28 ″
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical"
Xmlns: Android = "http://schemas.android.com/apk/res/android”>
<Textview
Android: Id = "@ + ID/namemessage"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "enter name:"> </textview>
<Edittext
Android: Id = "@ + ID/nameedittext"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textsize = "18sp"> </edittext>
<Linearlayout
Android: Id = "@ + ID/buttonlayout"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal">
<Button
Android: Id = "@ + ID/okbutton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "OK"> </button>
<Button
Android: Id = "@ + ID/cancelbutton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "cancel"> </button>
</Linearlayout>
</Linearlayout>

Next, apply the layout File above to our dialog box:

Class mydialog extends dialog {
....
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Log. D ("testapp", "dialog created ");
Setcontentview (R. layout. mydialog );
}
}

Now, we can call the show method of the dialog box class to display it:

...
Mydialog dialog = new mydialog (context );
Dialog. Show ();
...

The event processing mechanism of the dialog box component is the same as that of the activity. Let's take a look at how to handle the OK and cancle button events in the dialog box:

Class mydialog extends dialog implements onclicklistener {
Private button okbutton;
Private button cancelbutton;
Private edittext nameedittext;

Protected void oncreate (bundle savedinstancestate ){
Okbutton = (button) findviewbyid (R. Id. okbutton );
Cancelbutton = (button) findviewbyid (R. Id. cancelbutton );
Nameedittext = (edittext) findviewbyid (R. Id. nameedittext );
Okbutton. setonclicklistener (this );
Cancelbutton. setonclicklistener (this );
}

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

When the dialog box is closed, the dismiss () method of the dialog box class will be called. This method can be called by the dialog box itself or by other external code.
The "cancel" function is supported in the dialog box. "cancel" means that you no longer need to perform any functions and actions on the dialog box. You can call the cancel () method to cancel a dialog box. The cancel dialog box also automatically calls the dismiss () method.
When a user clicks the "return" button on the mobile phone device, the dialog box on the screen will be canceled. If you want to cancel the dialog box, you can set your dialog box as follows:

Setcancelable (false );

You can use the oncancellistener and ondismisslistener listeners to cancel and close the dialog box.

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 named 'builder'. The Builder class provides the ability to add multiple-choice or single-choice lists for the dialog box and add event processing 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>

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.