Android Suspension dialog (Point-and-click dialog box) implements code _android

Source: Internet
Author: User

Activity is one of the 4 application components of the Android system. The activity shown by the traditional method is full of the whole screen, that is, full screen activity. In fact, the activity can be displayed not only in full screen, but also directly on the screen like a dialog box. You can also close the activity by clicking anywhere on the screen, including within the activity and outside the activity. The traditional style of activity is an introductory technique for learning Android. Almost all beginners will learn from the activity. As a result, the component of the activity is more familiar to Android developers. Let's look at the basic configuration of the activity.

<activity android:name= ". Main "android:label=" @string/app_name ">
<intent-filter>
<action android:name=" Android.intent.action.MAIN "/>
<category android:name= android.intent.category.LAUNCHER"/>
< /intent-filter>
</activity>

The configuration code above is a typical activity configuration. Action and category are primarily specified in this configuration. The activity shown in this configuration will fill the entire screen. There are also many programs built into Android, most of which contain activity, for example, figure 1 is a clock program and a typical activity.


Suspension activity

The so-called suspended activity, is suspended on the desktop, looks like a dialog box. As shown in Figure 2.

In fact, implementing the above effect is not complicated, just add a android:theme attribute to the <activity> tag that defines the activity in the Androidmanifest.xml file, and specify the dialog box theme. The code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Net.blogjava.mobile "android:versioncode=" 1 "android:versionname=" 1.0 "> <application android:icon= "@drawable/date" android:label= "@string/app_name" > <activity android:name= ". Main "android:label=" @string/app_name "Android:theme=" @android: Style/theme.dialog "> <intent-filter> < Action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"
"/> </intent-filter> </activity> </application> <uses-sdk android:minsdkversion=" 3 "/> </manifest> <?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://" Schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" Fill_parent "Android: layout_height= "Fill_parent" > <textview android:layout_width= "fill_parent" android:layout_height= "WRAP_" Content "Android:text=" thisis a floating dialog box "android:layout_marginleft=" 20DP "android:layout_marginright=" 20DP "/> <linearlayout xmlns:android="
Http://schemas.android.com/apk/res/android "android:orientation=" horizontal "android:layout_width=" Fill_parent " android:layout_height= "fill_parent" android:gravity= "center" android:layout_margintop= "20DP" > <button Android:id= "@+id/btncurrentdate" android:layout_width= "100DP" android:layout_height= "Wrap_content" android:text= " Current Date "/> <button android:id=" @+id/btnfinish "android:layout_width=" 80DP "android:layout_height=" Wrap_content " android:text= "Off"/> </LinearLayout> </LinearLayout>

The Click event code for these two buttons is as follows:

public void OnClick (view view)
{
switch (View.getid ())
{case
r.id.btncurrentdate:
//Show current Date dialog box
SimpleDateFormat SimpleDateFormat = new SimpleDateFormat (
"Yyyy-mm-dd");
Datedialog.seticon (r.drawable.date);
Datedialog.settitle ("Current date:"
+ simpledateformat.format (new date ());
Datedialog.setbutton (OK), New Onclicklistener ()
{
@Override public
void OnClick (dialoginterface dialog, int which)
{
}
});
Datedialog.setondismisslistener (New Ondismisslistener ()
{
@Override public
void Ondismiss ( Dialoginterface dialog)
{
new Datedialog.builder (main.this). Setmessage (
"The current dialog box you have closed."). Create (). Show ();
}
};
Datedialog.show ();
break;
Case R.id.btnfinish:
//Close suspension activity
finish ();
break;
}
}

When you click the Show Date button, the effect is shown in Figure 4.

Touch a dialog box that can be closed anywhere

You usually need to click Close or other similar buttons to close the activity or dialog box. But sometimes you need to click anywhere on the screen to close the activity or dialog box. Closing the activity is fine, just deal with the event's touch events, and the code is as follows:

@Override Public
Boolean ontouchevent (Motionevent event)
{
finish ();
return true;
}

If you are a dialog box, you can also use the Ontouchevent event method. However, the general use of the Alertdialog dialog boxes are encapsulated well. Therefore, to use the Ontouchevent event method, you need to inherit the Alertdialog class. In the OnClick method given in the previous section, a Datedialog class is used in the code that pops up the current Display dialog box, which is a subclass of Alertdialog with the following code:

Package net.blogjava.mobile;
Import Android.app.AlertDialog;
Import Android.content.Context;
Import android.view.MotionEvent;
public class Datedialog extends Alertdialog
{public
Datedialog (context context)
{
Super );
}
@Override Public
Boolean ontouchevent (Motionevent event)
{
//Close the Display Date dialog
dismiss ();
Return Super.ontouchevent (event);
}

The Ontouchevent event method is also used in the above code. The dismiss method is called in the method to close the dialog box. The reader can run an example of this article to see if you can close the dialog box and suspend activity by clicking anywhere on the screen.

Summarize

This article describes the implementation of a dialog box that suspends activity and touches any location that can be closed. Suspension activity only needs to add Android:theme= "@android: Style/theme.dialog" to the <activity> element. To touch any location to close a dialog box or activity, you need to use a touch event (Ontouchevent method). If you are a dialog box, you need to use the Ontouchevent method by inheriting the Alertdialog class.

When you use the above configuration code, the activity shown is shown in Figure 2. In this example, two buttons were added to the activity to display the current date and close the dialog box.

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.