Android Development Timepicker Control usage examples detailed _android

Source: Internet
Author: User
Tags current time set time

The example of this article analyzes the Timepicker control usage of Android development. Share to everyone for your reference, specific as follows:

New project:

New Android project->
Project Name:hellospinner
Build Target:android 2.2
Application Name:hellospinner
Package name:com.b510
Create activity:mainactivity
Min SDK Version:9
Finish

Operation Effect:

If:

Return to New Timepickerdialog (this, Mtimesetlistener, Mhour, Mminute, false);

Code section:

Mainactivity.java:

Package com.b510;
Import Java.util.Calendar;
Import android.app.Activity;
Import Android.app.Dialog;
Import Android.app.TimePickerDialog;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;
Import Android.widget.TimePicker;
  public class Hellotimepicker extends activity {/* display time information/private TextView tvtimepickerdisplay;
  /* Set time button/Private button btntimepicker;
  * * hours */private int mhour;
  * * Minutes/private int mminute;
  /* Identity DIALOG ID * * static final int time_dialog_id = 0; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    This.tvtimepickerdisplay = (TextView) this. Findviewbyid (R.id.tv_timepicker_display);
    This.btntimepicker = (Button) Findviewbyid (R.id.btn_timepicker); Btntimepicker.setonclicklistener (Listener);
    Get the current time final Calendar C = calendar.getinstance ();
    Mhour = C.get (Calendar.hour_of_day);
    Mminute = C.get (Calendar.minute);
  Display the current date updatedisplay (); Private Onclicklistener listener = new Onclicklistener () {@Override public void OnClick (View v) {showd
    Ialog (time_dialog_id);
  }
  }; Updates the time we display in the TextView private void Updatedisplay () {Tvtimepickerdisplay.settext (new String
  Builder (). Append (Pad (mhour)). Append (":"). Append (Pad (mminute));
    private static String pad (int c) {if (c >=) return string.valueof (c);
  else return "0" + string.valueof (c); }//The callback received when the user "sets" the time in the dialog private Timepickerdialog.ontimesetlistener Mtim Esetlistener = new Timepickerdialog.ontimesetlistener () {public void Ontimeset (timepicker view, int hourofday, int mi
      Nute) {mhour = Hourofday; MmInute = minute;
    Updatedisplay ();
  }
  }; @Override protected Dialog oncreatedialog (int id) {switch (ID) {Case time_dialog_id:return new Timepick
    Erdialog (This, Mtimesetlistener, Mhour, Mminute, true);
  return null;

 }
}

Main.xml:

<?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:id= "@+id/tv_timepicker_display"
  android:layout_ Width= "Fill_parent" 
  android:layout_height= "wrap_content"/> <button android:id=
"@+id/
  " Btn_timepicker "
  android:layout_width=" fill_parent "
  android:layout_height=" Wrap_content "
  Android : text= "Change"
/>
</LinearLayout>

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", "Android Basic Components Usage Summary", " Android View tips Summary, Android layout layout tips and a summary of Android controls usage

I hope this article will help you with the Android program.

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.