热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

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

这篇文章主要为大家详细介绍了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日期时间选择器

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


推荐阅读
author-avatar
淡漠少_341
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有