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

China Automobile Dealers Association: Pickup Truck Market Analysis in June 2022

According to data from the Pickup Truck Market In...

Is cancer hereditary?

Recently, after giving birth to her second child,...

Hongmeng alone cannot save Huawei phones

In May 2019, facing overseas bans, Huawei solemnl...

How much does it cost to rent a server with 8 cores and 16G per year?

Server rental is divided into two categories: ind...

Brand marketing promotion: Why does Haidilao never advertise?

As a leader in the catering industry, Haidilao is...

Why didn’t QQ die out like MSN under the strong impact of WeChat?

QQ is the social platform with the largest number...

Welcome home! What innovative technologies will help Shenzhou 15 return home?

On June 4, the return capsule of the Shenzhou XV ...