package com.myapp.main;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.TextView;
import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.TimeZone;
/**
* Created by John on 2017/3/21.
* Description: Override Dialog
*/
public class MyDatePickerDialog extends Dialog implements NumberPicker.OnValueChangeListener, View.OnClickListener {
private Context context;
private OnSureListener onSureListener;
private TextView tv_title_year, tv_title_month;
private MyNumberPicker mnp_year, mnp_month;
private Button btn_sure, btn_cancel;
public MyDatePickerDialog(Context context){
super(context);
this.context = context;
}
public interface OnSureListener{
void back(String name);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_view);
tv_title_year = (TextView) findViewById(R.id.tv_title_year);
tv_title_month = (TextView) findViewById(R.id.tv_title_month);
mnp_year = (MyNumberPicker) findViewById(R.id.mnp_year);
mnp_month = (MyNumberPicker) findViewById(R.id.mnp_month);
btn_sure = (Button) findViewById(R.id.btn_sure);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
mnp_year.setOnValueChangedListener(this);
mnp_month.setOnValueChangedListener(this);
btn_sure.setOnClickListener(this);
btn_cancel.setOnClickListener(this);
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT + 08:00"));
mnp_year.setMaxValue(2050);
mnp_year.setMinValue(1900);
mnp_year.setValue(calendar.get(Calendar.YEAR));
tv_title_year.setText(String.valueOf(calendar.get(Calendar.YEAR)));
mnp_month.setMaxValue(12);
mnp_month.setMinValue(1);
mnp_month.setValue(calendar.get(Calendar.MONTH) + 1);
tv_title_month.setText(String.valueOf(calendar.get(Calendar.MONTH) + 1));
// set NumberPicker font color and divider color
setNumberPickerDividerColor(mnp_year, context.getResources().getColor(R.color.blue));
setNumberPickerTextColor(mnp_year, context.getResources().getColor(R.color.grey));
setNumberPickerDividerColor(mnp_month, context.getResources().getColor(R.color.blue));
setNumberPickerTextColor(mnp_month, context.getResources().getColor(R.color.grey));
// Prohibit rolling
mnp_year.setWrapSelectorWheel(false);
mnp_month.setWrapSelectorWheel(false);
}
public void setOnSureListener(OnSureListener onSureListener){
this.onSureListener = onSureListener;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_sure:
onSureListener.back(String.valueOf(tv_title_year.getText().toString() + "-" + tv_title_month.getText().toString()));
this.dismiss();
break;
case R.id.btn_cancel:
this.dismiss();
break;
}
}
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
switch (picker.getId()){
case R.id.mnp_year:
tv_title_year.setText(String.valueOf(newVal));
break;
case R.id.mnp_month:
tv_title_month.setText(String.valueOf(newVal));
break;
}
}
/**
* set NumberPicker font color
*
* @param numberPicker:NumberPicker
* @param color:int
* @return boolean
*/
public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
final int count = numberPicker.getChildCount();
for (int i = 0; i < count; i++) {
View child = numberPicker.getChildAt(i);
if (child instanceof EditText) {
Field selectorWheelPaintField;
try {
selectorWheelPaintField = numberPicker.getClass().getDeclaredField("mSelectorWheelPaint");
selectorWheelPaintField.setAccessible(true);
try {
((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
((EditText) child).setTextColor(color);
numberPicker.invalidate();
return true;
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
return false;
}
/**
* set NumberPicker divider color
*
* @param numberPicker:NumberPicker
* @param color:int
*/
public static void setNumberPickerDividerColor(NumberPicker numberPicker, int color) {
Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (Field SelectionDividerField : pickerFields) {
if (SelectionDividerField.getName().equals("mSelectionDivider")) {
SelectionDividerField.setAccessible(true);
try {
SelectionDividerField.set(numberPicker, new ColorDrawable(color));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
自定义 DatePicker 只显示选择年月
共73个文件
xml:38个
png:10个
bin:5个
1星 需积分: 5 70 下载量 57 浏览量
2017-03-21
14:22:21
上传
评论
收藏 753KB ZIP 举报
温馨提示
自定义选择年月的 DatePickerDialog,android 7.0系统也试用,不受系统版本限制
资源推荐
资源详情
资源评论
收起资源包目录
YMPickerDialog.zip (73个子文件)
YMPickerDialog
gradlew 5KB
settings.gradle 16B
.idea
libraries
junit_4_12.xml 477B
javax_inject_1.xml 324B
support_compat_25_3_0.xml 582B
support_annotations_25_3_0.xml 517B
javax_annotation_api_1_2.xml 358B
appcompat_v7_25_3_0.xml 576B
exposed_instrumentation_api_publish_0_5.xml 641B
hamcrest_library_1_3.xml 342B
espresso_idling_resource_2_2_2.xml 623B
constraint_layout_1_0_0_alpha9.xml 417B
hamcrest_core_1_3.xml 526B
support_vector_drawable_25_3_0.xml 609B
jsr305_2_0_1.xml 330B
constraint_layout_solver_1_0_0_alpha9.xml 369B
support_core_ui_25_3_0.xml 585B
hamcrest_integration_1_3.xml 354B
support_fragment_25_3_0.xml 588B
espresso_core_2_2_2.xml 590B
support_core_utils_25_3_0.xml 594B
rules_0_5.xml 551B
support_media_compat_25_3_0.xml 600B
support_v4_25_3_0.xml 404B
javawriter_2_1_1.xml 330B
animated_vector_drawable_25_3_0.xml 612B
runner_0_5.xml 554B
runConfigurations.xml 564B
misc.xml 2KB
compiler.xml 686B
workspace.xml 168KB
gradle.xml 626B
modules.xml 365B
copyright
profiles_settings.xml 76B
gradle
wrapper
gradle-wrapper.properties 236B
gradle-wrapper.jar 52KB
YMPickerDialog.iml 868B
.gradle
3.3
tasks
_app_compileDebugJavaWithJavac
localJarClasspathSnapshot
localJarClasspathSnapshot.bin 20KB
localJarClasspathSnapshot.lock 17B
localClassSetAnalysis
localClassSetAnalysis.bin 51KB
localClassSetAnalysis.lock 17B
taskArtifacts
fileHashes.bin 273KB
taskArtifacts.bin 122KB
fileSnapshots.bin 3.19MB
taskArtifacts.lock 17B
local.properties 439B
gradlew.bat 2KB
gradle.properties 747B
.gitignore 127B
app
src
main
AndroidManifest.xml 728B
res
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 4KB
mipmap-xxxhdpi
ic_launcher.png 10KB
ic_launcher_round.png 14KB
mipmap-xhdpi
ic_launcher.png 5KB
ic_launcher_round.png 6KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 2KB
drawable
layout
dialog_view.xml 3KB
activity_main.xml 569B
values
colors.xml 294B
strings.xml 201B
styles.xml 380B
mipmap-xxhdpi
ic_launcher.png 8KB
ic_launcher_round.png 10KB
java
com
myapp
main
MyDatePickerDialog.java 6KB
MainActivity.java 1KB
MyNumberPicker.java 1KB
libs
app.iml 11KB
.gitignore 8B
build.gradle 968B
proguard-rules.pro 952B
build.gradle 521B
共 73 条
- 1
资源评论
- mhtc50522017-11-14忽悠,代码和描述的不是一个东西
秋水渡人
- 粉丝: 1
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
- 基于python实现的水下压缩空气储能互补系统建模仿真与经济效益分析+源代码+论文
- 华中科技大学-自然语言处理实验,Bi-LSTM+CRF的中文分词框架,并且利用基于深度学习的方法进行中文命名实体识别++源码报告
- 基于动态罚函数的铁路车流分配与径路优化模型python源码
- 鱼群算法求解组环问题python源码+文档说明
- 基于决策优化的多波束测深测线规划模型MATLAB代码
- 课程设计-基于python实现的多目标优化算法求解带时间窗的车辆路径规划问题+源代码+文档说明+界面截图+pptx
- 基于通信信号与通信系统的MATLAB仿真源码-课程设计
- 嵌入式-信号机制(概念,发送,定时,捕捉,SIGCHLD 信号实现回收子进程)
- c语言管理系统大一大二笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功