Android Date Time Picker

Android Date Time Picker

[[185911]]

Many applications have date pickers, such as setting the start and end time of some tasks. For the convenience of users and the good-looking interface, many applications use date pickers. I looked online. I personally feel that many date pickers are not very good-looking, but it is also a bit troublesome to modify them, so I wrote a demo. As for the interface effect, I personally feel that it is also very low-end. After all, I am not a UI expert, so I will just make do with it. These are not important because they can be modified.

If you want to implement the year, month, and day, please see the content in the note below. The figure below is the implementation of the minute 00 15 30 45. If you want 0-59, please see the content in the note below.

If you need an iOS-like app with the day of the week

First, the interface pops up the PopupWindow animation. The specific code is as follows

Enter the animation

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. < set xmlns:android= "http://schemas.android.com/apk/res/android" >
  3.  
  4. <translate
  5. android:duration= "500"  
  6. android:fromYDelta= "100.0%p"  
  7. android:toYDelta= "45" />
  8.  
  9. </ set >

Exit Animation

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. < set xmlns:android= "http://schemas.android.com/apk/res/android" >
  3.  
  4. <translate
  5. android:duration= "500"  
  6. android:fromYDelta= "0.0"  
  7. android:toYDelta= "100.0%p" />
  8.  
  9. </ set >

Contents of the main interface

  1. public class MainActivity extends Activity implements View .OnClickListener{
  2. private TextView tv_house_time;
  3. private TextView tv_center;
  4. private WheelMain wheelMainDate;
  5. private String beginTime;
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. // TODO Auto-generated method stub
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. initView();
  13. initEvent();
  14. }
  15.  
  16. private void initEvent() {
  17. tv_house_time.setOnClickListener(this);
  18. }
  19.  
  20. private void initView() {
  21. tv_house_time = (TextView) findViewById(R.id.tv_house_time);
  22. tv_center = (TextView) findViewById(R.id.tv_center);
  23. }
  24.  
  25. private java.text.DateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
  26. public void showBottoPopupWindow() {
  27. WindowManager manager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
  28. Display defaultDisplay = manager.getDefaultDisplay();
  29. DisplayMetrics outMetrics = new DisplayMetrics();
  30. defaultDisplay.getMetrics(outMetrics);
  31. int width = outMetrics.widthPixels;
  32. View menuView = LayoutInflater. from (this).inflate(R.layout.show_popup_window, null );
  33. final PopupWindow mPopupWindow = new PopupWindow(menuView, ( int )(width*0.8),
  34. ActionBar.LayoutParams.WRAP_CONTENT);
  35. ScreenInfo screenInfoDate = new ScreenInfo(this);
  36. wheelMainDate = new WheelMain(menuView, true );
  37. wheelMainDate.screenheight = screenInfoDate.getHeight();
  38. String time = DateUtils.currentMonth().toString();
  39. Calendar calendar = Calendar.getInstance();
  40. if (JudgeDate.isDate( time , "yyyy-MM-DD" )) {
  41. try {
  42. calendar.setTime(new Date ( time ));
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. int   year = calendar.get ( Calendar.YEAR );
  48. int   month = calendar.get ( Calendar.MONTH );
  49. int   day = calendar.get(Calendar.DAY_OF_MONTH);
  50. int hours = calendar.get(Calendar.HOUR_OF_DAY);
  51. int   minute = calendar.get(Calendar. MINUTE );
  52. wheelMainDate.initDateTimePicker( year , month , day , hours, minute );
  53. final String currentTime = wheelMainDate.getTime().toString();
  54. mPopupWindow.setAnimationStyle(R.style.AnimationPreview);
  55. mPopupWindow.setTouchable( true );
  56. mPopupWindow.setFocusable( true );
  57. mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
  58. mPopupWindow.showAtLocation(tv_center, Gravity.CENTER, 0, 0);
  59. mPopupWindow.setOnDismissListener(new poponDismissListener());
  60. backgroundAlpha(0.6f);
  61. TextView tv_cancle = (TextView) menuView.findViewById(R.id.tv_cancle);
  62. TextView tv_ensure = (TextView) menuView.findViewById(R.id.tv_ensure);
  63. TextView tv_pop_title = (TextView) menuView.findViewById(R.id.tv_pop_title);
  64. tv_pop_title.setText( "Select start time" );
  65. tv_cancle.setOnClickListener(new View .OnClickListener() {
  66. @Override
  67. public void onClick( View arg0) {
  68. mPopupWindow.dismiss();
  69. backgroundAlpha(1f);
  70. }
  71. });
  72. tv_ensure.setOnClickListener(new View .OnClickListener() {
  73.  
  74. @Override
  75. public void onClick( View arg0) {
  76. beginTime = wheelMainDate.getTime().toString();
  77. try {
  78. Date   begin = dateFormat.parse(currentTime);
  79. Date   end = dateFormat.parse(beginTime);
  80. tv_house_time.setText(DateUtils.currentTimeDeatil( begin ));
  81. } catch (ParseException e) {
  82. e.printStackTrace();
  83. }
  84. mPopupWindow.dismiss();
  85. backgroundAlpha(1f);
  86. }
  87. });
  88. }
  89.  
  90. public void backgroundAlpha( float bgAlpha) {
  91. WindowManager.LayoutParams lp = getWindow().getAttributes();
  92. lp.alpha = bgAlpha;
  93. getWindow().setAttributes(lp);
  94. }
  95.  
  96. @Override
  97. public void onClick( View v) {
  98. switch (v.getId()){
  99. case R.id.tv_house_time:
  100. showBottoPopupWindow();
  101. break;
  102. }
  103. }
  104.  
  105. class poponDismissListener implements PopupWindow.OnDismissListener {
  106. @Override
  107. public void onDismiss() {
  108. backgroundAlpha(1f);
  109. }
  110.  
  111. }
  112. }

Layout content

  1. <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"  
  2. android:id= "@+id/rel_select"  
  3. android:layout_width= "wrap_content"  
  4. android:layout_height= "wrap_content"  
  5. android:layout_centerInParent= "true"  
  6. android:layout_margin= "10dp"  
  7. android:background= "@drawable/border_circle_radius"  
  8. android:orientation= "vertical" >
  9. <TextView
  10. android:background= "#2F0F9980"  
  11. android:padding= "10dp"  
  12. android:id= "@+id/tv_pop_title"  
  13. android:textSize= "18sp"  
  14. android:gravity= "center"  
  15. android:textColor= "#301616"  
  16. android:layout_width= "match_parent"  
  17. android:layout_height= "wrap_content" />
  18. <LinearLayout
  19. android:id= "@+id/timePicker1"  
  20. android:layout_width= "match_parent"  
  21. android:layout_height= "wrap_content"  
  22. android:orientation= "horizontal" >
  23. <liuyongxiang.robert.com.testtime.wheelview.WheelView
  24. android:id= "@+id/year"  
  25. android:layout_width= "wrap_content"  
  26. android:layout_weight= "1"  
  27. android:layout_height= "wrap_content" />
  28. <liuyongxiang.robert.com.testtime.wheelview.DashedLineView
  29. android:layout_width= "1dp"  
  30. android:gravity= "center_vertical"  
  31. android:layout_gravity= "center"  
  32. android:background= "@drawable/dotted_line"  
  33. android:layout_height= "match_parent" />
  34. <liuyongxiang.robert.com.testtime.wheelview.WheelView
  35. android:id= "@+id/month"  
  36. android:layout_width= "wrap_content"  
  37. android:layout_weight= "1"  
  38. android:layout_height= "wrap_content" />
  39. <liuyongxiang.robert.com.testtime.wheelview.DashedLineView
  40. android:layout_width= "1dp"  
  41. android:gravity= "center_vertical"  
  42. android:layout_gravity= "center"  
  43. android:background= "@drawable/dotted_line"  
  44. android:layout_height= "match_parent" />
  45. <liuyongxiang.robert.com.testtime.wheelview.WheelView
  46. android:id= "@+id/day"  
  47. android:layout_width= "wrap_content"  
  48. android:layout_weight= "1"  
  49. android:layout_height= "wrap_content" />
  50. <liuyongxiang.robert.com.testtime.wheelview.DashedLineView
  51. android:layout_width= "1dp"  
  52. android:background= "@drawable/dotted_line"  
  53. android:layout_height= "match_parent" />
  54. <liuyongxiang.robert.com.testtime.wheelview.WheelView
  55. android:id= "@+id/hour"  
  56. android:layout_width= "wrap_content"  
  57. android:layout_weight= "1"  
  58. android:layout_height= "wrap_content" />
  59. <liuyongxiang.robert.com.testtime.wheelview.DashedLineView
  60. android:layout_width= "1dp"  
  61. android:background= "@drawable/dotted_line"  
  62. android:layout_height= "match_parent" />
  63. <liuyongxiang.robert.com.testtime.wheelview.WheelView
  64. android:id= "@+id/mins"  
  65. android:layout_width= "wrap_content"  
  66. android:layout_weight= "1"  
  67. android:layout_height= "wrap_content" />
  68. </LinearLayout>
  69. <LinearLayout
  70. android:layout_width= "match_parent"  
  71. android:layout_height= "wrap_content"  
  72. android:background= "#2F0F9980"  
  73. android:padding= "10dp"  
  74. android:orientation= "horizontal" >
  75.  
  76. <TextView
  77. android:id= "@+id/tv_cancle"  
  78. android:layout_width= "0dp"  
  79. android:layout_height= "wrap_content"  
  80. android:gravity= "center"  
  81. android:paddingLeft= "20dp"  
  82. android:paddingRight= "20dp"  
  83. android:padding= "5dp"  
  84. android:textSize= "18sp"  
  85. android:textColor= "#ff0000"  
  86. android:layout_weight= "1"  
  87. android:background= "@drawable/btn_pop"  
  88. android:text= "Cancel" />
  89.  
  90. <TextView
  91. android:layout_width= "0dp"  
  92. android:layout_height= "wrap_content"  
  93. android:layout_weight= "3" />
  94.  
  95. <TextView
  96. android:id= "@+id/tv_ensure"  
  97. android:layout_width= "0dp"  
  98. android:layout_height= "wrap_content"  
  99. android:gravity= "center"  
  100. android:textColor= "#414341"  
  101. android:padding= "5dp"  
  102. android:paddingLeft= "20dp"  
  103. android:paddingRight= "20dp"  
  104. android:layout_weight= "1"  
  105. android:textSize= "18sp"  
  106. android:background= "@drawable/btn_pop"  
  107. android:text= "OK" />
  108. </LinearLayout>
  109.  
  110. </LinearLayout>

Please note

The time display in MainActivity, tv_house_time.setText(DateUtils.currentTimeDeatil(begin)); needs to be changed to

tv_house_time.setText(DateUtils.formateStringH(beginTime,DateUtils.yyyyMMddHHmm)); Otherwise the actual time is 00:00

Modified

Change the following code in WheelMain

  1. wv_mins.setAdapter(adapter);
  2. wv_mins.setCyclic( true ); // Cyclic scrolling
  3. wv_mins.setLabel("分"); // Add text
  4. int   min = setMinute(m);
  5. wv_mins.setCurrentItem( min );

Replace with

  1. wv_mins.setAdapter(new NumericWheelAdapter(
  2. 0, 59));
  3. wv_mins.setCyclic( true ); // Cyclic scrolling
  4. wv_mins.setLabel("分"); // Add text
  5. wv_mins.setCurrentItem(m);

It is also necessary to

  1. int   minute = Integer .valueOf(adapter.getItem(wv_mins.getCurrentItem()));

Change to

  1. int   minute = wv_mins.getCurrentItem();

Will change the minute from 0 to 59

If you don't want the time but only the year, month and day, you only need

  1. if (hasSelectTime) {
  2.  
  3. wv_hours.setVisibility( View .GONE);
  4.  
  5. wv_mins.setVisibility( View .GONE);
  6.  
  7. } else {
  8.  
  9. wv_hours.setVisibility( View .GONE);
  10.  
  11. wv_mins.setVisibility( View .GONE);
  12.  
  13. wv_day.setVisibility( View .GONE);
  14.  
  15. }

Just release this code and remove the code in the green area below.

You also need to change the following code in MainActivty

  1. wheelMainDate.initDateTimePicker( year , month , day , hours, minute );

Change to

  1. wheelMainDate.initDateTimePicker( year , month , day );

And wheelMain

  1. if (!hasSelectTime) {
  2. sb.append((wv_year.getCurrentItem() + START_YEAR)).append( "-" )
  3. .append(strMon).append( "-" )
  4. .append(strDay).append( " " ).append(strHour).append( ":" ).append(strMin);
  5. } else {
  6. sb.append((wv_year.getCurrentItem() + START_YEAR)).append( "-" )
  7. .append(strMon).append( "-" )
  8. .append(strDay).append( " " ).append(strHour).append( ":" ).append(strMin);
  9. }

Need to be modified to

  1. if (!hasSelectTime) {
  2. sb.append((wv_year.getCurrentItem() + START_YEAR)).append( "-" )
  3. .append(strMon).append( "-" )
  4. .append(strDay);
  5. } else {
  6. sb.append((wv_year.getCurrentItem() + START_YEAR)).append( "-" )
  7. .append(strMon).append( "-" )
  8. .append(strDay);
  9. }

The effect is as follows

<<:  Breadth-first search algorithm applied to Swift mobile game development

>>:  Common macros for iOS development

Recommend

How to play Baidu bidding ocpc second level

For SEMers who often play with small Baidu accoun...

Modern climate change: Missing a woman, missing out on a discipline

Science has no national boundaries and no gender ...

Which of the commonly used apps' memory usage drives you crazy?

Many users with limited memory should have encoun...

Functional Animation in UX Design

[[149375]] A good UX designer can easily explain ...

How dangerous are “killer bees”? Can they be killed?

When it comes to killer bees, what's the firs...

How to choose a TV at this stage? Don’t buy 4K, 1080p is the best

It's the TV buying season again, and the &quo...

Tablet market is a mixed bag: iPad downturn and China's rise

Although Apple's iPad sales have declined for ...