欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android仿iPhone日期时间选择器详解

程序员文章站 2023-11-06 22:02:46
本文实例为大家分享了android仿iphone时间选择器的具体代码,供大家参考,具体内容如下 先看效果图 如何使用 import java.text...

本文实例为大家分享了android仿iphone时间选择器的具体代码,供大家参考,具体内容如下

先看效果图

Android仿iPhone日期时间选择器详解

如何使用

import java.text.dateformat; 
import java.text.parseexception; 
import java.text.simpledateformat; 
import java.util.calendar; 
 
import android.app.activity; 
import android.app.alertdialog; 
import android.content.dialoginterface; 
import android.os.bundle; 
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; 
 dateformat dateformat = new simpledateformat("yyyy-mm-dd"); 
 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  txttime = (edittext) findviewbyid(r.id.txttime); 
  calendar calendar = calendar.getinstance(); 
  txttime.settext(calendar.get(calendar.year) + "-" 
    + (calendar.get(calendar.month) + 1) + "-" 
    + calendar.get(calendar.day_of_month) + ""); 
  button btnselecttime = (button) findviewbyid(r.id.button1); 
  btnselecttime.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, true); 
    wheelmain.screenheight = screeninfo.getheight(); 
    string time = txttime.gettext().tostring(); 
    calendar calendar = calendar.getinstance(); 
    if (judgedate.isdate(time, "yyyy-mm-dd")) { 
     try { 
      calendar.settime(dateformat.parse(time)); 
     } catch (parseexception e) { 
      // todo auto-generated catch block 
      e.printstacktrace(); 
     } 
    } 
    int year = calendar.get(calendar.year); 
    int month = calendar.get(calendar.month); 
    int day = calendar.get(calendar.day_of_month); 
    int h = calendar.gettime().gethours(); 
    int m = calendar.gettime().getminutes(); 
    wheelmain.initdatetimepicker(year, month, day, h, m); 
    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("取消", 
        new dialoginterface.onclicklistener() { 
         @override 
         public void onclick(dialoginterface dialog, 
           int which) { 
         } 
        }).show(); 
   } 
  }); 
 } 
} 

源码下载:仿iphone日期时间选择器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。