Android source code: Custom date and time control (SelectTime)

Android source code: Custom date and time control (SelectTime)

Functional classification: Life

Supported platforms: Android

Operating environment: Eclipse

Development language: Java

Development tool: Eclipse

Source code size: 90.47KB

Source code download address: http://down..com/data/1977586

Source code introduction

Attached is the source code of a custom date and time control (SelectTime) for the Android platform.

From Android Bus address: http://www.apkbus.com/forum.php?mod=viewthread&tid=174518&extra=

Source code running screenshot

Source code snippet

  1. package com.widget.time;
  2.   
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6.   
  7. import android.app.Activity;
  8. import android.app.AlertDialog;
  9. import android.content.DialogInterface;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.EditText;
  17.   
  18. public   class MainActivity extends Activity {
  19. WheelMain wheelMain;
  20. EditText txttime;
  21.   
  22. Button btnselecttime1, btnselecttime2, btnselecttime3;
  23.   
  24. int year, month, day, hour, min;
  25.   
  26. LayoutInflater inflater;
  27.   
  28. @Override  
  29. public   void onCreate(Bundle savedInstanceState) {
  30. super .onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. txttime = (EditText) findViewById(R.id.txttime);
  33. btnselecttime1 = (Button) findViewById(R.id.button1);
  34. btnselecttime2 = (Button) findViewById(R.id.button2);
  35. btnselecttime3 = (Button) findViewById(R.id.button3);
  36.   
  37. String yyyy = null ;
  38. SimpleDateFormat formatter = new SimpleDateFormat( "yyyy" );
  39. Date curDate = new Date(System.currentTimeMillis()); // Get the current time  
  40. yyyy = formatter.format(curDate);
  41.           
  42. Log.e( "-------------" , yyyy);
  43.   
  44. Calendar calendar = Calendar.getInstance();
  45.   
  46. year = calendar.get(Calendar.YEAR);
  47. month = calendar.get(Calendar.MONTH);
  48. day = calendar.get(Calendar.DAY_OF_MONTH);
  49. hour = calendar.get(Calendar.HOUR_OF_DAY);
  50. min = calendar.get(Calendar.MINUTE);
  51.   
  52. inflater = LayoutInflater.from(MainActivity. this );
  53.   
  54. btnselecttime1.setOnClickListener( new OnClickListener() {
  55.   
  56. @Override  
  57. public   void onClick(View arg0) {
  58.   
  59. final View timepickerview = inflater.inflate(
  60. R.layout.timepicker, null );
  61. ScreenInfo screenInfo = new ScreenInfo(MainActivity. this );
  62.   
  63. wheelMain = new WheelMain(timepickerview, 0 );
  64. wheelMain.screenheight = screenInfo.getHeight();
  65. wheelMain.initDateTimePicker(year, month, day, hour, min);
  66.   
  67. new AlertDialog.Builder(MainActivity. this )
  68. .setTitle( "Select time" )
  69. .setView(timepickerview)
  70. .setPositiveButton( "OK" ,
  71. new DialogInterface.OnClickListener() {
  72. @Override  
  73. public   void onClick(DialogInterface dialog,
  74. int which) {
  75. txttime.setText(wheelMain.getTime());
  76. }
  77. }).setNegativeButton( "Cancel" , null ).show();
  78.   
  79. }
  80. });
  81.   
  82. btnselecttime2.setOnClickListener( new OnClickListener() {
  83.   
  84. @Override  
  85. public   void onClick(View arg0) {
  86. LayoutInflater inflater = LayoutInflater
  87. .from(MainActivity. this );
  88. final View timepickerview = inflater.inflate(
  89. R.layout.timepicker, null );
  90. ScreenInfo screenInfo = new ScreenInfo(MainActivity. this );
  91.   
  92. wheelMain = new WheelMain(timepickerview, 1 );
  93. wheelMain.screenheight = screenInfo.getHeight();
  94. wheelMain.initDateTimePicker(year, month, day, hour, min);
  95.   
  96. new AlertDialog.Builder(MainActivity. this )
  97. .setTitle( "Select Date" )
  98. .setView(timepickerview)
  99. .setPositiveButton( "OK" ,
  100. new DialogInterface.OnClickListener() {
  101. @Override  
  102. public   void onClick(DialogInterface dialog,
  103. int which) {
  104. txttime.setText(wheelMain.getTime());
  105. }
  106. }).setNegativeButton( "Cancel" , null ).show();
  107.   
  108. }
  109. });
  110.   
  111. btnselecttime3.setOnClickListener( new OnClickListener() {
  112.   
  113. @Override  
  114. public   void onClick(View arg0) {
  115. LayoutInflater inflater = LayoutInflater
  116. .from(MainActivity. this );
  117. final View timepickerview = inflater.inflate(
  118. R.layout.timepicker, null );
  119. ScreenInfo screenInfo = new ScreenInfo(MainActivity. this );
  120.   
  121. wheelMain = new WheelMain(timepickerview, 2 );
  122. wheelMain.screenheight = screenInfo.getHeight();
  123. wheelMain.initDateTimePicker(year, month, day, hour, min);
  124.   
  125. new AlertDialog.Builder(MainActivity. this )
  126. .setTitle( "Select Year" )
  127. .setView(timepickerview)
  128. .setPositiveButton( "OK" ,
  129. new DialogInterface.OnClickListener() {
  130. @Override  
  131. public   void onClick(DialogInterface dialog,
  132. int which) {
  133. txttime.setText(wheelMain.getTime());
  134. }
  135. }).setNegativeButton( "Cancel" , null ).show();
  136.   
  137. }
  138. });
  139. }
  140. }

Source code download address: http://down..com/data/1977586

<<:  Android source code download: Android-like Tiantian Dongting player

>>:  15 Essential Development Tools for Android Java Programmers

Recommend

Golden Apple Snail: Double "Killer" of Rice Fields and Health

Recently, many netizens posted on social media th...

You may not know that iPhone can also scan QR codes like this

If I tell you that the iPhone's native camera...

30 years of smart home development: a booming industry

Industrial chain integrates manufacturing and ser...

4 essential elements of an event operation plan!

A complete activity plan should have the followin...

Click, click! Scientists decode sperm whales' mysterious phonetic alphabet

Produced by: Science Popularization China Author:...

What is the bridge of communication between "Striver" and "Exploration One"?

Improving the scientific literacy of all citizens...

A must-read for APP operators, 108 examples of marketing tactics!

Introduction: In the recent Double Eleven e-comme...