Android源码:自定义日期时间控件(SelectTime)

移动开发 Android
附件为Android平台的一个自定义日期时间控件(SelectTime) 的源码。

功能分类:生活

支持平台:Android

运行环境:Eclipse

开发语言:Java

开发工具:Eclipse

源码大小:90.47KB

源码下载地址:http://down.51cto.com/data/1977586

源码简介

 

附件为Android平台的一个自定义日期时间控件(SelectTime) 的源码。

来自安卓巴士地址:http://www.apkbus.com/forum.php?mod=viewthread&tid=174518&extra=

源码运行截图

源码片段

  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());// 获取当前时间 
  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("选择时间"
  69.                         .setView(timepickerview) 
  70.                         .setPositiveButton("确定"
  71.                                 new DialogInterface.OnClickListener() { 
  72.                                     @Override 
  73.                                     public void onClick(DialogInterface dialog, 
  74.                                             int which) { 
  75.                                         txttime.setText(wheelMain.getTime()); 
  76.                                     } 
  77.                                 }).setNegativeButton("取消"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("选择日期"
  98.                         .setView(timepickerview) 
  99.                         .setPositiveButton("确定"
  100.                                 new DialogInterface.OnClickListener() { 
  101.                                     @Override 
  102.                                     public void onClick(DialogInterface dialog, 
  103.                                             int which) { 
  104.                                         txttime.setText(wheelMain.getTime()); 
  105.                                     } 
  106.                                 }).setNegativeButton("取消"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("选择年份"
  127.                         .setView(timepickerview) 
  128.                         .setPositiveButton("确定"
  129.                                 new DialogInterface.OnClickListener() { 
  130.                                     @Override 
  131.                                     public void onClick(DialogInterface dialog, 
  132.                                             int which) { 
  133.                                         txttime.setText(wheelMain.getTime()); 
  134.                                     } 
  135.                                 }).setNegativeButton("取消"null).show(); 
  136.   
  137.             } 
  138.         }); 
  139.     } 

源码下载地址:http://down.51cto.com/data/1977586

责任编辑:闫佳明 来源: 网络整理
相关推荐

2015-02-11 17:49:35

Android源码自定义控件

2009-07-31 10:23:09

ASP.NET源码DateTimePic

2014-09-24 11:42:46

AndroidButton

2009-06-08 20:13:36

Eclipse自定义控

2017-02-17 09:37:12

Android自定义控件方法总结

2013-04-19 10:14:24

2016-12-26 15:25:59

Android自定义View

2009-09-03 13:34:03

.NET自定义控件

2017-04-21 14:27:21

Android控件QQ未读消息

2021-08-16 14:45:38

鸿蒙HarmonyOS应用

2009-08-06 17:13:56

ASP.NET自定义控

2009-02-10 12:55:39

自定义控件AJAX.NET

2009-08-05 17:03:37

C#自定义控件

2011-07-05 18:51:51

QT 控件 鼠标

2021-08-25 10:14:51

鸿蒙HarmonyOS应用

2013-04-01 14:35:10

Android开发Android自定义x

2016-11-16 21:55:55

源码分析自定义view androi

2009-08-03 13:39:46

C#自定义用户控件

2009-08-03 13:34:06

自定义C#控件

2021-09-06 14:58:23

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号