We create a new project with Androidstudio, select the blank template, and then drag it into two buttons, name their IDs btdate (display date), Bttime (display time), his template XML code is very simple
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.neil.ad01.MainActivity"> One A - <Button - Android:layout_width= "Wrap_content" the 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" /> + A <Button at Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "Show Current Time" - Android:id= "@+id/bttime" - Android:layout_below= "@+id/btdate" - Android:layout_alignparentleft= "true" in Android:layout_alignparentstart= "true" /> - </Relativelayout>
A standard Android application window class needs to inherit the Android.app.Activity class, at least implement the OnCreate method to initialize the window. Next Implementation method
1 Packagecom.neil.ad01;2 3 Importandroid.app.Activity;4 ImportAndroid.app.AlertDialog;5 Importandroid.content.Intent;6 ImportAndroid.os.Bundle;7 ImportAndroid.view.View;8 ImportAndroid.widget.Button;9 Ten ImportJava.text.SimpleDateFormat; One Importjava.util.Date; A - Public classMainactivityextendsActivityImplementsView.onclicklistener - { the - Private voidShowDialog (String title,string msg) - { -Alertdialog.builder builder=NewAlertdialog.builder ( This); + //Setting the icon for a dialog box - Builder.seticon (Android. R.drawable.ic_dialog_info); + //setting the caption of a dialog box A Builder.settitle (title); at //setting information for a dialog box - builder.setmessage (msg); - //Set button for dialog box -Builder.setpositivebutton ("OK",NULL); - //Show dialog Box - builder.create (). Show (); in - Intent Intent; to } + //Initializing Windows - @Override the protected voidonCreate (Bundle savedinstancestate) { * Super. OnCreate (savedinstancestate); $ //loaded view (the XML file you just wrote)Panax Notoginseng Setcontentview (r.layout.activity_main); - //get an instance of two button objects theButton btdate=(Button) Findviewbyid (r.id.btdate); +Button bttime=(Button) Findviewbyid (r.id.bttime); A //two buttons to add a Click event Listener (object that implements the Onclicklistener interface) theBtdate.setonclicklistener ( This); +Bttime.setonclicklistener ( This); - } $ $ //two buttons share a single click event, which distinguishes which button was clicked by the button's ID - @Override - Public voidOnClick (View v) { the Switch(V.getid ()) - {Wuyi Caser.id.btdate: { theSimpleDateFormat SDF =NewSimpleDateFormat ("Yyyy-mm-dd"); - //Show Current Date WuShowDialog ("Current Date", Sdf.format (NewDate ())); - Break; About } $ Caser.id.bttime: { -SimpleDateFormat SDF =NewSimpleDateFormat ("HH:mm:ss"); - //Show Current Date -ShowDialog ("Current Time", Sdf.format (NewDate ())); A Break; + } the } - } $}
Note: 1,alertdialog can be used to display dialog boxes
2, if multiple controls share an event method, the Android:id property of the control label must be specified 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 in the Gen directory to identify the current resource file. Therefore, in the OnCreate method, you can refer to the Activity_main.xml file by R.layout.activity_main, which means that a variable of type static int called Activity_main is generated in the layout subclass of the R class. , the code for the layout class is as follows
4, if you use a click event, you must implement the Onclicklistener interface, the OnClick method of the interface is to click the event callback method
Any window class in an Android application must be defined in the Androidmanifest.xml file, otherwise it cannot be used. The Android:label property of the <activity> tag uses a string resource when defining the Mainactivity class. The Andrdoid:label property value for the <application> tag that defines information about the Android application also uses a string resource
The Androidmanifest.xml code is as follows
<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "COM.NEIL.AD01"> <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme"> <ActivityAndroid:name=". Mainactivity "Android:label= "@string/title_activity_main"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid: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 is as follows
< resources > < string name = "App_name" > Display Date Time Span style= "color: #0000ff;" ></ string > < string name = "Title_activity_main" > displays datetime string > </ resources >
At this point, the Code section is all written.
Then click the Run button to generate the app in the simulator,
This simulator is generated in Qt for Android, Androidstudio comes with the simulator said to shut down the Hyper-V virtual machine in Windows system, also Hmax Intel Accelerator, install Genymotion also can not run, Installed domestic simulator simply Androidstudio can not be identified, it is dizzy +_+, fortunately, Qt to force, later can also use QT based on C + + development Android,vs now also realize VC + + development Android, now the interaction between various platforms is really more and more to force. Look forward to a bigger breakthrough!!!
This Android tutorial is also for beginners, because I am also a novice, haha
(Android Learning series) One, use the button to achieve the time display