"Android" date picker, time Picker and menu

Source: Internet
Author: User

In the Android 4.4 project, each new project will also appear HelloWorld, but different from the previous version, now HelloWorld also comes with a menu. This article works with Android's common component date picker and time picker to explain how the Android menu is modified. Date picker, time picker two components very large, in general, it is recommended to use the "Android" between multiple activity using the bundle transfer value (click the open link) described in the way, for the date picker, time picker to open a new activity to interact.

An app like:


Once you enter the app, the current time is displayed in the lower right corner.

By moving the date picker, the time picker, the lower right corner of the time will be modified.

At the same time, the new app, in the upper right corner of the own menu, modified to bring a "quit" subkey, close the app.

1, first, the res\value\strings.xml, the definition of the string, modified to the following code. The main is to modify the name of the app with the children of the menu.

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "app_name" > Picker and Menu </string >    <string name= "Menu_exit" > Exit </string></resources>
2, then modify the Res\menu\main.xml, define the child of the upper right menu, modify the code as follows, define the child's ID is menu_exit, and its display font is a string of menu_exit strings, that is, "exit" word.

<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item        android:id= "@+id/menu_ Exit "        android:title=" @string/menu_exit "/></menu>
3, the next is to modify the Res\layout\activity_main.xml, the layout of the Mainactivity.java, here the layout thought is as follows, wherein, in the linear layout of the components are centered, that is, set android:layout_gravity= "Center_vertical".


Therefore, modify the Res\layout\activity_main.xml as follows to give the ID to the component to be manipulated:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" > <datepicker android:id= "@+id/date Picker1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_grav Ity= "Center_vertical" android:calendarviewshown= "false"/> <linearlayout android:layout_width= "MATC            H_parent "android:layout_height=" wrap_content "android:orientation=" Horizontal "> <timepicker Android:id= "@+id/timepicker1" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "android:layout_gravity=" center_vertical "/> <linearlayout android:layout_w            Idth= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_vertical" Android:orientation= "vertical" > <textview android:id= "@+id/textview1" Android:layout_width= "W Rap_content "android:layout_height=" wrap_content "android:textsize=" 18sp "/> & Lt TextView android:id= "@+id/textview2" android:layout_width= "Wrap_content" Android            oid:layout_height= "Wrap_content" android:textsize= "18sp"/> </LinearLayout> </LinearLayout></LinearLayout>
Where the date picker setting: Android:calendarviewshown= "false" is set to not display its own date component.

Time Picker default is 12-hour system, where the effect is 24 hours is set in Mainactivity.java.

Date picker, time picker is not recommended to put on one line, these two components do not have their own size property to adjust its size. If you want to adjust its size, write a very code in Java, and the information circulating on the Internet is not valid with Android Api8, that is, android2.2. This is not recommended here.

4, finally, write the following code to Mainactivity.java, here the code is divided into two parts, part of the Mainactivity.java internal date picker, time picker, part of the menu code. Where the date of the current system is removed and minutes and seconds are used to the calendar instance, that is, "Java" uses the Calendar class to get the current date (click the Open link) in the method. We don't have to repeat it here.

Package com. Picker;import Java.util.calendar;import Java.util.gregoriancalendar;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.menuitem;import Android.widget.DatePicker; Import Android.widget.datepicker.ondatechangedlistener;import Android.widget.textview;import Android.widget.timepicker;import Android.widget.timepicker.ontimechangedlistener;public class MainActivity extends Activity {private DatePicker datepicker1;private timepicker timepicker1;private TextView textview1;private TextView textView2; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);//Take out the current month and day calendar calendar = calendar.getinstance (); int year = Calendar.get (gregoriancalendar.year); int month = Calendar.get (gregoriancalendar.month) + 1;int day = Calendar.get ( gregoriancalendar.day_of_month); int hour = Calendar.get (gregoriancalendar.hour); int minutes = Calendar.get ( Gregoriancalendar.minutE);//Get each component TextView1 = (TextView) Findviewbyid (r.id.textview1); textView2 = (TextView) Findviewbyid (R.ID.TEXTVIEW2); DatePicker1 = (DatePicker) Findviewbyid (r.id.datepicker1); timePicker1 = (Timepicker) Findviewbyid (r.id.timepicker1) ;//Set the time picker to 24-hour Timepicker1.setis24hourview (true);//The program starts by initializing two label text and setting it to the current period. Textview1.settext (Year + "years" + month + "Month" + Day + "Days"), Textview2.settext (Hour + ":" + minutes),//Set listener for date picker, note its Setup method and other build , you need to set Datepicker1.init (year, month, day, new Ondatechangedlistener () {@Overridepublic void ondatechanged by its initialization method ( DatePicker arg0, int year, Int. Month,int Day) {//If it is modified, then change the label Text 1, where the month is also +1 is equal to the normal month textview1.settext (year + "years" + (Mo nth+1) + "Month" + Day + "Days");}); /Time Picker Modified listener Timepicker1.setontimechangedlistener (new Ontimechangedlistener () {@Overridepublic void ontimechanged ( Timepicker arg0, int hour, int minutes) {//once modified, the label text is modified 2textview2.settext (hour + ":" + minutes);}});} You create a method for the menu without the method, and the menu is not set in the upper-right corner. @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {// Set the menu interface to Res\menu\menu.xmlgetmenuinflater (). Inflate (R.menu.main, menu); return true;} Handle Menu Event Public boolean onoptionsitemselected (MenuItem Item) {//Gets the Id,int item_id = Item.getitemid () of the currently selected MenuItem; Switch (item_id) {//sets the method to be executed for the menu subkey with ID menu_exit. Case R.id.menu_exit:finish ();//close mainactivity this activity. Equivalent to the end of the program, written system.exit (0) can also. break;} return true;}}

"Android" date picker, time Picker and menu

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.