跟别的网盘相比,天翼云盘和百度网盘有什么优势?

当前位置: >
在Android上的日期或时间样式选择
时间: 17:39 来源:互联网 作者:源码搜藏 浏览:
源码类别:时间Time|Date
源码大小:未知
编码格式:gbk
授权方式:免费源码
运行环境:php+mysql
官方网址:暂无
欢迎加入QQ群讨论学习
材料日期时间选择器试图所示为您提供的日期和时间选择器的材料设计规范,有一个简单的主题化API。库使用从为基础,并调整它是尽可能接近到材料设计的例子。
支持Android 4.0及了。
随意叉子或发布在github上拉请求。问题可以在GitHub的问题跟踪报告。
日期选取器
时间选择器
使用材料的日期/时间选择器
实施监听器
建立拾荒者
潜在的改进
到材质的DateTime选取器库添加到您的项目最简单的方法是添加它作为一个依赖你&build.gradle
dependencies {
compile 'com.wdullaer:materialdatetimepicker:2.5.0'
您也可以作为一个Android库添加库到您的项目。所有的库文件住library。
使用材料的日期/时间选择器
图书馆遵循相同的API在Android框架等采摘。对于基本实现,你需要
实施OnTimeSetListener/OnDateSetListener
创建TimePickerDialog/&DatePickerDialog使用附带的工厂
实施OnTimeSetListener/OnDateSetListener
为了接收在选择器设置的日期或时间,你将需要实现OnTimeSetListener或&OnDateSetListener接口。通常,这将是Activity或Fragment创建该选取器。回调使用相同的API标准Android采摘。
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
String time = &You picked the following time: &+hourOfDay+&h&+
timeTextView.setText(time);
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = &You picked the following date: &+dayOfMonth+&/&+(monthOfYear+1)+&/&+
dateTextView.setText(date);
创建TimePickerDialog/&DatePickerDialog使用附带的工厂
您将需要创建一个新的实例TimePickerDialog或DatePickerDialog使用静态newInstance()方法,提供合适的默认值和回调。一旦对话框配置,你可以调用show()。
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
dpd.show(getFragmentManager(), &Datepickerdialog&);
采摘将自动根据在那里它们被创建的,根据当前的当前主题进行主题colorAccent。您还可以通过主题在对话框setAccentColor(int color)的方法。或者,您可以主题采摘通过覆盖颜色资源mdtp_accent_color,并mdtp_accent_color_dark在您的项目。
&color name=&mdtp_accent_color&&#009688&/color&
&color name=&mdtp_accent_color_dark&&#00796b&/color&
其中颜色被选择的确切顺序如下:
setAccentColor(int color)&在Java代码
android.R.attr.colorAccent&(如果Android 5.0以上版本)
R.attr.colorAccent&(例如使用应用程序兼容性时)
R.color.mdtp_accent_color而R.color.mdtp_accent_color_dark如果没有其他人在你的项目设置
采摘也有黑暗的主题。这可以globablly使用指定mdtp_theme_dark在你的主题或属性setThemeDark(boolean themeDark)的功能。该函数调用覆盖XML设置。
&item name=&mdtp_theme_dark&&true&/item&
TimePickerDialog深色主题
的TimePickerDialog拥有,可以通过调用设置一个黑暗的主题
tpd.setThemeDark(true);
DatePickerDialog深色主题的DatePickerDialog拥有,可以通过调用设置一个黑暗的主题
dpd.setThemeDark(true);
setAccentColor(String color)并setAccentColor(int color)&设置为通过对话中使用的强调色彩。字符串版本解析出彩使用Color.parseColor()。的int版本需要ColorInt字节字符串。这将明确设置的颜色完全不透明。
TimePickerDialog&setTitle(String title)
示出了在所述顶部的标题&TimePickerDialog
DatePickerDialog&setTitle(String title)&示出了在所述顶部的标题DatePickerDialog,而不是星期几
setOkText()并setCancelText()&设置对话框确定自定义文本和取消标签。可以接受字符串的资源ID。工程中都DatePickerDialog和TimePickerDialog
setMinTime(Timepoint time)
设定的最低有效时间被选择。时间值在当天早些时候将被停用
setMaxTime(Timepoint time)
设置最大有效时间被选择。在一天的时间值之后将被停用
setSelectableTimes(Timepoint[] times)&您可以在一个阵列传递Timepoints。这些值在选择器的唯一有效的选择。setMinTime(Timepoint time)而且setMaxTime(Timepoint time),将进一步削减这个名单了。
setTimeInterval(int hourInterval, int minuteInterval, int secondInterval)&设置在TimePickerDialog可选择倍的间隔。这是一个方便的包装器setSelectableTimes
setSelectableDays(Calendar[] days)
你可以传递一个Calendar[]到DatePickerDialog。在此列表中的数值为选择器唯一可接受的日期。它优先setMinDate(Calendar day)和setMaxDate(Calendar day)
setDisabledDays(Calendar[] days)
在这个值Calendar[]被明确禁用(不可选)。此选项可以一起搭配使用setSelectableDays(Calendar[] days):万一有冲突setDisabledDays(Calendar[] days)将优先setSelectableDays(Calendar[] days)
setHighlightedDays(Calendar[] days)
你可以通过一个Calendar[]天来突出。他们会以粗体呈现。您可以通过覆盖调整的突出天的颜色mdtp_date_picker_text_highlighted
showYearPickerFirst(boolean yearPicker)
显示今年第一选择器,而不是月份和日期选择器。
OnDismissListener而OnCancelListener
这两种选择器可以通过一个DialogInterface.OnDismissLisener或DialogInterface.OnCancelListener它允许你在发生这些事件的运行代码。
tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialogInterface) {
Log.d(&TimePicker&, &Dialog was cancelled&);
vibrate(boolean vibrate)
置时做出选择对话框是否应当震动设备。默认为true。
dismissOnPause(boolean dismissOnPause)
设置选择器是否驳回本身当父活动被暂停或是否重新创建自己当活动重新开始。
DatePickerDialog&autoDismiss(boolean autoDismiss)&如果设置为true当用户选择一个日期将关闭该选择器。默认为false。
TimepickerDialog&enableSeconds(boolean enableSconds)并enableMinutes(boolean enableMinutes)&允许您启用或禁用秒和分钟选择器ONT他TimepickerDialog。启用秒选择器,意味着使分钟选择器。禁用分钟选择器将禁用秒选择器。最后应用的设置将被使用。默认情况下enableSeconds = false和
转载请注明出处
源码搜藏网所有源码来自用户上传分享,版权问题及牵扯到商业纠纷均与源码搜藏网无关安卓(Android)实现选择时间功能
由于大部分android初级教程都没教怎么选择时间,初学者碰到这种难免会有些不知所措,难道要让用户自己输入日期时间?先不说用户体验不好,处理用户输入各式各样的日期格式也要花好大一番功夫,下面看看如何实现android选择时间的功能。
android 6.0 中的运行效果
TimePickerDialog和DatePickerDialog介绍
系统封装好了两个类可以供我们直接调用,TimepickerDialog用于选择时间,DatePickerDialog用于选择日期。
TimePikckerDialog的构造方法
public TimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView)
&&&& 1、第一个参数接受一个context信息
&&&& 2、第二个参数为当选择时间完成后执行的回调接口
&&&& 3、第三个参数和第四个参数为初始化的时间
&&&& 4、第四个参数选择true代表24小时制,false代表12小时制
DatePickerDialog构造方法
public DatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)
&&&&& 1、第一个参数接受context信息
&&&&& 2、第二个参数为日期选择完成后的回掉接口
&&&&& 3、最后三个参数分别为初始化的年月日
可以看出两者的构造方法基本相差不多,由于两者都是继承自AlertDialog,所以得到两者对象后只要调用它们的show()方法即可将选择框弹出。
有两种实现方式,一种是直接在Activity中使用,还有一种是通过FragmentDialog使用。
直接在Activity中使用比较简单,不过代码会比较乱,通过FragmentDialog管理的使用方式会比较优雅,而且便于管理。
直接在Activity中使用
布局文件,里面就一个TextView用于显示所选时间
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testdemo.TestActivity"&
android:layout_centerInParent="true"
android:textSize="20sp"
android:id="@+id/time_text"
android:text="点此选择时间"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /&
&/RelativeLayout&
Activity文件:
public class TestActivity extends AppCompatActivity {
private TextView timeT
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
timeText = (TextView) findViewById(R.id.time_text);
//为TextView设置点击事件
timeText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//将timeText传入用于显示所选择的时间
showDialogPick((TextView) v);
//将两个选择时间的dialog放在该函数中
private void showDialogPick(final TextView timeText) {
final StringBuffer time = new StringBuffer();
//获取Calendar对象,用于获取当前时间
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
//实例化TimePickerDialog对象
final TimePickerDialog timePickerDialog = new TimePickerDialog(TestActivity.this, new TimePickerDialog.OnTimeSetListener() {
//选择完时间后会调用该回调函数
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.append(" " + hourOfDay + ":" + minute);
//设置TextView显示最终选择的时间
timeText.setText(time);
}, hour, minute, true);
//实例化DatePickerDialog对象
DatePickerDialog datePickerDialog = new DatePickerDialog(TestActivity.this, new DatePickerDialog.OnDateSetListener() {
//选择完日期后会调用该回调函数
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//因为monthOfYear会比实际月份少一月所以这边要加1
time.append(year + "-" + (monthOfYear+1) + "-" + dayOfMonth);
//选择完日期后弹出选择时间对话框
timePickerDialog.show();
}, year, month, day);
//弹出选择日期对话框
datePickerDialog.show();
到此,点击运行就可以看效果了:)
通过FragmentDialog使用
为什么要用DialogFragment
&&&&& 1、用DialogFragment管理对话框是官方推介的使用方式。
&&&&&& 2、使用DialogFragment管理对话框也方便代码的重用。
通过FragmentDialog实现步骤
DatePickerFragment类:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
public Dialog onCreateDialog(Bundle savedInstanceState) {
//得到Calendar类实例,用于获取当前时间
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
//返回DatePickerDialog对象
//因为实现了OnDateSetListener接口,所以第二个参数直接传入this
return new DatePickerDialog(getActivity(), this, year, month, day);
//实现OnDateSetListener接口的onDateSet()方法
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//这样子写就将选择时间的fragment和选择日期的fragment完全绑定在一起
//使用的时候只需直接调用DatePickerFragment的show()方法
//即可选择完日期后选择时间
TimePickerFragment timePicker = new TimePickerFragment();
timePicker.show(getFragmentManager(), "time_picker");
//将用户选择的日期传到TimePickerFragment
date = year + "年" + (monthOfYear+1) + "月" + dayOfMonth + "日";
timePicker.setTime(date);
TimePickerFragment类:
//实现OnTimeSetListener接口
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
private String time = "";
public Dialog onCreateDialog(Bundle savedInstanceState) {
//新建日历类用于获取当前时间
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
//返回TimePickerDialog对象
//因为实现了OnTimeSetListener接口,所以第二个参数直接传入this
return new TimePickerDialog(getActivity(), this, hour, minute, true);
//实现OnTimeSetListener的onTimeSet方法
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
//判断activity是否是DataCallBack的一个实例
if(getActivity() instanceof DataCallBack){
//将activity强转为DataCallBack
DataCallBack dataCallBack = (DataCallBack) getActivity();
time = time + hourOfDay + "点" + minute + "分";
//调用activity的getData方法将数据传回activity显示
dataCallBack.getData(time);
public void setTime(String date){
Activity的布局文件,只有一个TextView用于显示时间
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testdemo.TestActivityActivity"&
android:id="@+id/time_text"
android:layout_centerInParent="true"
android:text="点此选择时间"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /&
&/RelativeLayout&
Activity文件:
//实现DataCallBack接口,实现与Fragment的通信
public class TestActivityActivity extends AppCompatActivity implements DataCallBack{
TextView timeT
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
timeText = (TextView) findViewById(R.id.time_text);
//为timeText设置点击事件
timeText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//实例化对象
DatePickerFragment datePickerFragment = new DatePickerFragment();
//调用show方法弹出对话框
// 第一个参数为FragmentManager对象
// 第二个为调用该方法的fragment的标签
datePickerFragment.show(getFragmentManager(), "date_picker");
//实现DataCallBack的getData方法
public void getData(String data) {
//data即为fragment调用该函数传回的日期时间
timeText.setText(data);
由于TimePickerFragment对话框是在DatePickerFragment类里面启动的,所以这样写只能日期和时间都选择,如果要单独选择日期或者时间,只需要重写onTimeSet()或者onDateSet()方法即可
兼容性问题
不同的android版本显示的效果不同,在android6.0效果很好,不过在一些低版本android(如4.0,笔者没有每个版本都测试)会出现调用两次回掉函数的情况,导致选择两次时间。解决的办法有很多,只要保证回调函数里面的逻辑只执行一次就可以。这里提供一种比较通用的方法。
重写TimePickerDialog和DatePickerDialog的onStop()方法
直接在Activity中使用的重写方法
final TimePickerDialog timePickerDialog = new TimePickerDialog(TestActivity.this, new TimePickerDialog.OnTimeSetListener() {
//选择完时间后会调用该回调函数
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
time.append(" " + hourOfDay + ":" + minute);
//设置TextView显示最终选择的时间
timeText.setText(time);
}, hour, minute, true){
// 重写onStop()
protected void onStop() {
//实例化DatePickerDialog对象
DatePickerDialog datePickerDialog = new DatePickerDialog(TestActivity.this, new DatePickerDialog.OnDateSetListener() {
//选择完日期后会调用该回调函数
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//因为monthOfYear会比实际月份少一月所以这边要加1
time.append(year + "-" + (monthOfYear+1) + "-" + dayOfMonth);
//选择完日期后弹出选择时间对话框
timePickerDialog.show();
}, year, month, day){
//重写onstop
protected void onStop() {
上面的写法看起来会比较乱,也可以另外新建一个类继承TimePickerDialog或者DatePickerDialog然后重写onStop()方法
通过FragmentDialog使用的重写方式
只需在onCreateDialog()方法里面重写即可,下面的代码会比较清晰
return new DatePickerDialog(getActivity(), this, year, month, day){
// 重写onStop
protected void onStop() {
return new TimePickerDialog(getActivity(), this, hour, minute, true){
//重写onStop
protected void onStop() {
笔者水平有限,但是保证以上代码都是亲手实现过一遍的。如果有什么不足之处欢迎大家指出,以上就是本文的全部内容,希望对大家开发Android能有所帮助。
顶一下(0) 踩一下(0)
热门标签:3300人阅读
Android学习笔记(20)
根据业务需求,需要实现如题的功能,细想一下,其中包含很多细节,如下:
1,如果设置年大于当前的年,那么月,日,时间可以随便设置;
2,如果设置年小于当前的年,那么则设置不能成功;
3,如果设置年等于当前的年,那么如果设置月大于当前的月,那么日,时间可以随便设置;
4,如果设置年等于当前的年,那么如果设置月小于当前的月,那么则设置不能成功;
5,如果设置年等于当前的年,那么如果设置月等于当前的月,那么如果设置日大于当前的日,则时间可以随便设置;
。。。。。。
在android中,关于日期和时间,我们首先想到的便是DatePicker和TimePicker两个组件,接下来便是实现方式的问题,该如何实现呢?我本来设想的是用DatePickerDialog和TimePickerDialog,然后通过判断来决定DatePicker中的按钮是否可按,比如,设置年的值如果要小于当前的年的时候,则DatePicker中的“-”按钮则是不可按的。这种方式实现起来比较的麻烦,而且源码里面有些东西也没看懂,就自己做了个类似DatePickerDialog的组件,如下图:
今日是6号,“-”按钮是没有反映的,点击一下则变成了灰色,按照上面的业务一步一步的分析,则可以实现,不难,但是很烦琐特别是布局,最讨厌的就是调整布局,所以就把这些逻辑还是放到原有的DatePicker上来实现。当然实现的界面可能就没有这么直观了。
& & & & & & & & & & & & & & & & & & & & & 1 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & 2
& & & & & & & & & & & & & & & & & & & & & & & & & 3 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & &4
& & & & & & & & & & & & & & & & & & & & & & & & 5 & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & & 6
& & & & & & & & & & & & & & & & & & & & & & & & & & &7
可能感觉有点坑爹,但是还是将就吧。
看看关键的代码:
protected Dialog onCreateDialog(int id) {
Dialog dialog =
switch (id) {
case R.id.wait_edit_yuyuedata:
DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datePicker,int year, int month, int dayOfMonth) {
Log.i(TAG,&onDateSet-----------&year:&+year+&,month:&+(month+1)+&,day:&+dayOfMonth);
if(year&getYear()){ //设置年大于当前年,直接设置,不用判断下面的
wait_edit_yuyuedata.setText(dayOfMonth + &/&+(month+1)+&/&+year);
}else if(year == getYear()){ //设置年等于当前年,则向下开始判断月
if((month+1) & getMonth()){ //设置月等于当前月,直接设置,不用判断下面的
wait_edit_yuyuedata.setText(dayOfMonth + &/&+(month+1)+&/&+year);
}else if((month+1) == getMonth()){
//设置月等于当前月,则向下开始判断日
if(dayOfMonth & getDay()){
//设置日大于当前月,直接设置,不用判断下面的
wait_edit_yuyuedata.setText(dayOfMonth + &/&+(month+1)+&/&+year);
}else if(dayOfMonth == getDay()){ //设置日等于当前日,则向下开始判断时
wait_edit_yuyuedata.setText(dayOfMonth + &/&+(month+1)+&/&+year);
//设置日小于当前日,提示重新设置
Toast.makeText(MainActivity.this, &当前日不能小于今日,请重新设置&, 2000).show();
//设置月小于当前月,提示重新设置
Toast.makeText(MainActivity.this, &当前月不能小于今月,请重新设置&, 2000).show();
//设置年小于当前年,提示重新设置
Toast.makeText(MainActivity.this, &当前年不能小于今年,请重新设置&, 2000).show();
if(flag == 3){
datePicker.init(getYear(), (getMonth()-1), getDay(), new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
wait_edit_yuyuedata.setText(dayOfMonth + &/&+(monthOfYear+1)+&/&+year);
dialog = new DatePickerDialog(this, dateListener, getYear(), (getMonth()-1), getDay());
case R.id.wait_edit_arriveltime:
TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Log.i(TAG,&onTimeSet-----------&flag:&+flag+&,hourOfDay:&+hourOfDay+&,minute:&+minute);
switch (flag) {
//设置日期在当前日期之后,直接设置时间,不用判断
wait_edit_arriveltime.setText(hourOfDay+&:&+minute);
//设置日期等于当前日期之后,判断时和分
if(hourOfDay&getHonor()){
wait_edit_arriveltime.setText(hourOfDay+&:&+minute);
}else if(hourOfDay == getHonor()){
if(minute&getMin()){
wait_edit_arriveltime.setText(hourOfDay+&:&+minute);
wait_edit_arriveltime.setText(getHonor()+&:&+getMin());
Toast.makeText(MainActivity.this, &请选择大于现在时刻的分钟&, 2000).show();
wait_edit_arriveltime.setText(getHonor()+&:&+getMin());
Toast.makeText(MainActivity.this, &请选择大于现在时刻的小时&, 2000).show();
//设置日期等于当前日期之前,提示日期还未设置正确,不能设置时间
wait_edit_arriveltime.setText(getHonor()+&:&+getMin());
Toast.makeText(MainActivity.this, &请先设置正确的日期&, 2000).show();
Log.i(TAG,&init-----------&flag:&+flag+&,hourOfDay:&+getHonor()+&,minute:&+getMin());
dialog = new TimePickerDialog(this, timeListener, getHonor(),getMin(), true);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(&您要确定放弃编辑吗?&);
builder.setPositiveButton(&确定&,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
MainActivity.this.finish();
builder.setNegativeButton(&取消&,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
builder.show();
具体的代码,请点击下载:1:& 2:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:251558次
积分:2887
积分:2887
排名:第10345名
原创:47篇
转载:20篇
评论:125条
(1)(1)(4)(7)(5)(6)(2)(7)(13)(21)

我要回帖

更多关于 天翼云盘和百度网盘 的文章

 

随机推荐