Android仿iPhone日期时间选择器详解
作者:DylanAndroid 时间:2023-09-11 04:23:37
本文实例为大家分享了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日期时间选择器
来源:http://blog.csdn.net/linglongxin24/article/details/37933527
标签:Android,iPhone,选择器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入分析NTFS中文件被锁定导致Process.Start失败的详解
2023-08-24 06:11:25
![](https://img.aspxhome.com/file/2023/8/94658_0s.png)
Android引导页面的简单实现
2023-08-06 04:24:32
![](https://img.aspxhome.com/file/2023/3/86013_0s.gif)
Java 字符串反转实现代码
2023-01-09 10:04:52
c# 遍历获取所有文件的示例代码
2022-11-21 20:01:43
Java8 Comparator: 列表排序的深入讲解
2022-12-11 08:30:01
Unity通用泛型单例设计模式(普通型和继承自MonoBehaviour)
2023-08-24 14:53:28
spring 注解如何开启声明式事务
2023-04-09 23:07:42
Spring Security+JWT简述(附源码)
2022-10-16 11:57:51
![](https://img.aspxhome.com/file/2023/1/108481_0s.png)
Java设计模式之桥接模式
2021-08-31 05:56:02
Android通过自定义View实现随机验证码
2022-10-24 07:16:19
![](https://img.aspxhome.com/file/2023/7/104657_0s.png)
如何为Repository添加自定义方法
2022-05-13 20:08:24
![](https://img.aspxhome.com/file/2023/4/88214_0s.png)
spring中12种@Transactional的失效场景(小结)
2022-04-26 00:06:40
![](https://img.aspxhome.com/file/2023/3/103993_0s.jpg)
详解C语言内核字符串转换方法
2022-12-09 13:53:55
![](https://img.aspxhome.com/file/2023/6/109766_0s.png)
聊一聊SpringBoot服务监控机制
2023-02-09 02:47:48
![](https://img.aspxhome.com/file/2023/9/64169_0s.png)
C#实现多线程的Web代理服务器实例
2022-02-25 13:32:08
java如何给对象按照字符串属性进行排序
2022-05-23 02:17:14
Java使用Collections.sort()排序的方法
2022-09-11 18:47:55
java 使用HttpURLConnection发送数据简单实例
2023-12-12 01:44:00
Idea配置超详细图文教程(2020.2版本)
2023-03-13 21:41:04
![](https://img.aspxhome.com/file/2023/6/108376_0s.png)
Java C++实现相同MD5加密算法的方式
2023-10-12 12:26:02
![](https://img.aspxhome.com/file/2023/5/58685_0s.jpg)