功能分类:生活
支持平台: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=
源码运行截图
源码片段
- package com.widget.time;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends Activity {
- WheelMain wheelMain;
- EditText txttime;
- Button btnselecttime1, btnselecttime2, btnselecttime3;
- int year, month, day, hour, min;
- LayoutInflater inflater;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- txttime = (EditText) findViewById(R.id.txttime);
- btnselecttime1 = (Button) findViewById(R.id.button1);
- btnselecttime2 = (Button) findViewById(R.id.button2);
- btnselecttime3 = (Button) findViewById(R.id.button3);
- String yyyy = null;
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
- Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
- yyyy = formatter.format(curDate);
- Log.e("-------------", yyyy);
- Calendar calendar = Calendar.getInstance();
- year = calendar.get(Calendar.YEAR);
- month = calendar.get(Calendar.MONTH);
- day = calendar.get(Calendar.DAY_OF_MONTH);
- hour = calendar.get(Calendar.HOUR_OF_DAY);
- min = calendar.get(Calendar.MINUTE);
- inflater = LayoutInflater.from(MainActivity.this);
- btnselecttime1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- final View timepickerview = inflater.inflate(
- R.layout.timepicker, null);
- ScreenInfo screenInfo = new ScreenInfo(MainActivity.this);
- wheelMain = new WheelMain(timepickerview, 0);
- wheelMain.screenheight = screenInfo.getHeight();
- wheelMain.initDateTimePicker(year, month, day, hour, min);
- new AlertDialog.Builder(MainActivity.this)
- .setTitle("选择时间")
- .setView(timepickerview)
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- txttime.setText(wheelMain.getTime());
- }
- }).setNegativeButton("取消", null).show();
- }
- });
- btnselecttime2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- LayoutInflater inflater = LayoutInflater
- .from(MainActivity.this);
- final View timepickerview = inflater.inflate(
- R.layout.timepicker, null);
- ScreenInfo screenInfo = new ScreenInfo(MainActivity.this);
- wheelMain = new WheelMain(timepickerview, 1);
- wheelMain.screenheight = screenInfo.getHeight();
- wheelMain.initDateTimePicker(year, month, day, hour, min);
- new AlertDialog.Builder(MainActivity.this)
- .setTitle("选择日期")
- .setView(timepickerview)
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- txttime.setText(wheelMain.getTime());
- }
- }).setNegativeButton("取消", null).show();
- }
- });
- btnselecttime3.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- LayoutInflater inflater = LayoutInflater
- .from(MainActivity.this);
- final View timepickerview = inflater.inflate(
- R.layout.timepicker, null);
- ScreenInfo screenInfo = new ScreenInfo(MainActivity.this);
- wheelMain = new WheelMain(timepickerview, 2);
- wheelMain.screenheight = screenInfo.getHeight();
- wheelMain.initDateTimePicker(year, month, day, hour, min);
- new AlertDialog.Builder(MainActivity.this)
- .setTitle("选择年份")
- .setView(timepickerview)
- .setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- txttime.setText(wheelMain.getTime());
- }
- }).setNegativeButton("取消", null).show();
- }
- });
- }
- }