Android Learning Series A button to display time _android

Source: Internet
Author: User
Tags control label event listener

We first use Androidstudio to create a new project, select a blank template, and then drag the two button into it, name their IDs btdate (display date), Bttime (Show time), and his template XML code is simple

<?xml version= "." Encoding= "utf-"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_
Vertical_margin "tools:context=" com.neil.ad.MainActivity "> <button android:layout_width=" wrap_content " android:layout_height= "Wrap_content" android:text= "Show Current date" android:id= "@+id/btdate" Android:layout_ Alignparentleft= "true" android:layout_alignparentstart= "true" android:layout_alignparenttop= "true"/> < Button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "shows the current time" android:id= " @+id/bttime "android:layout_below=" @+id/btdate "android:layout_alignparentleft=" true "Android:layout_ Alignparentstart= "true"/> </RelativeLayout> 

As shown in the figure


A standard Android application window class needs to inherit the Android.app.Activity class, and at least implement the OnCreate method to initialize the window. Next Implementation method

Package com.neil.ad;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Java.text.SimpleDateFormat;
Import Java.util.Date; public class Mainactivity extends activity implements View.onclicklistener {private void ShowDialog (String title,string m SG) {Alertdialog.builder builder=new alertdialog.builder (this);//Set the icon for the dialog box Builder.seticon (Android.
R.drawable.ic_dialog_info);
Set the title of the dialog box Builder.settitle (title);
Sets the information for the dialog box Builder.setmessage (msg);
Sets the button Builder.setpositivebutton for the dialog box (OK, null);
Display dialog Box Builder.create (). Show ();
Intent Intent; }//Initialization window @Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);//Loaded view (
The XML file just written) Setcontentview (R.layout.activity_main);
Gets an instance of the two button object button btdate= (button) Findviewbyid (r.id.btdate);
Button bttime= (button) Findviewbyid (r.id.bttime); Add a click event listener for two buttons (implements the object of the Onclicklistener interface) btDate.setonclicklistener (this);
Bttime.setonclicklistener (this); ///Two buttons share a click event, distinguishing which button is clicked by the ID of the button @Override public void OnClick (View v) {switch (V.getid ()) {case r.id.btdate: {simpled
Ateformat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Displays the current date ShowDialog ("Current Date", Sdf.format (new Date ());
Break  Case R.id.bttime: {simpledateformat sdf = new SimpleDateFormat ("HH:mm:ss");//show Current date ShowDialog ("Current Time", Sdf.format (new
Date ()));
Break }
}
}
}

Note:

1,alertdialog can be used to display dialog boxes

2, if multiple controls share an event method, you must specify the Android:id property of the control label in the layout file, and the ID property of each control cannot be the same

Each resource file in the 3.res (Resource) directory generates a variable of type int in the R class under the Gen directory to identify the current resource file. So in the OnCreate method you can refer to the Activity_main.xml file by R.layout.activity_main, which means that a variable of the static int type named Activity_main has been generated in the layout subclass of the R class , the code for the layout class is as follows

4, if the Click event is used, the Onclicklistener interface must be implemented, and the OnClick method of the interface is to click the event callback method

Any window class in the Android application must be defined in the Androidmanifest.xml file, otherwise it cannot be used. A string resource was used when defining the Mainactivity Class <activity> label Android:label properties. Andrdoid:label property values for <application> tags that define information about Android applications also use string resources

The Androidmanifest.xml code is as follows

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
package=" com.neil.ad01 ">
<application
android:allowbackup=" true "
android:icon=" @mipmap/ic_launcher "
android:label=" @string/app_name " 
android:supportsrtl=" true "
android:theme=" @ Style/apptheme ">
<activity android:name=". Mainactivity "
android:label=" @string/title_activity_main ">
<intent-filter>
<action Android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER" "/>
</intent-filter>
</activity>
</application>
</manifest>

It can also change the value of Android:label in String.xml, string.xml content as follows

<resources>
<string name= "App_name" > Show Date Time </string>
<string name= "Title_activity_ Main "> Show Date Time </string>
</resources>

At this point, the Code section is all written out.

Then click the Run button to generate the app in the emulator, as shown in

Androidstudio with the simulator said to shut down the Windows system of Hyper-V virtual machine, but also Hmax Intel Accelerator, installed Genymotion also can not run, installed domestic simulator simply Androidstudio can not recognize, really dizzy + _+, fortunately, Qt to force, can also use QT based on C + + development Android,vs now also realized the VC + + development of Android, now the interaction between various platforms is really more and more to the force. Look forward to a larger breakthrough!!!

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.