TimePicker,TimePickerDialog以及自定义timepicker(一)
场景:在activity上点击,弹出一个dialog,然后点击dialog上的一个按钮,在弹出时间。以及自定义dialog
懒,要用到一个选择时间的需求,只要求小时和分钟,弹出式,第一时间想到了timepicker以及numberpicker,
先用timepicker,发现在4.0黑色丑死了,4.4正常想要的以及5.0上是钟摆,用
timepickerdialog比timepicker多个上午和下午,算了还是自定义吧:
很晚了,直接贴代码关于布局等后面会仔细研究学习的:
主activity:
package com.example.onebelowzero;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private MyCustomDialog dialog;
private void init() {
mBtn = (Button) findViewById(R.id.btn);
mBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn:
doIt();
break;
default:
break;
}
}
private void doIt(){
/**
* 此时样式好丑,我们在清单文件中改下主题即可 不一定要在appli。。上改,在activity上改即可
* android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.Fullscreen"
*/
dialog = new MyCustomDialog(this, R.style.CustomDialog);
dialog.setContentView(R.layout.dialog_mycustom);
dialog.show();
}
}
=====================
MyCustomDialog :
package com.example.onebelowzero;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;
import com.example.onebelowzero.MyPickerView.onSelectListener;
/**
自定义dialog
*/
public class MyCustomDialog extends Dialog implements android.view.View.OnClickListener{
private Context context;
private LeaveMyDialogListener listener;
public interface LeaveMyDialogListener {
public void onClick(View view);
public void refreshUI(String timer);
}
public MyCustomDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
public MyCustomDialog(Context context, int theme) {
super(context, theme);
// TODO Auto-generated constructor stub
this.context = context;
}
public MyCustomDialog(Context context, int theme, LeaveMyDialogListener listener) {
super(context, theme);
// TODO Auto-generated constructor stub
this.context = context;
this.listener = listener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initView();
}
private void initView() {
// TODO Auto-generated method stub
Button showPicker = (Button) findViewById(R.id.dialog_activity);
showPicker.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dateDialog();
// dateDialog2();
// dateDialog3();
}
/**
* 弹出4.0,4.4,5.0TimePicker样式各不一样
*/
private void dateDialog() {
TimePickerDialog time = new TimePickerDialog(context,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
Toast.makeText(context,
hourOfDay + "hour " + minute + "minute",
Toast.LENGTH_SHORT).show();
}
}, 18, 25, true);
time.show();
}
/**
* 方式二和方式一差不多
*/
private void dateDialog2() {
final TimePicker mDatePicker = new TimePicker(context);
AlertDialog.Builder timeDialog = new AlertDialog.Builder(context);
// timeDialog.setTitle("一座城池2012");
timeDialog.setIcon(null);
timeDialog.setCancelable(false);
timeDialog.setView(mDatePicker);
timeDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 取出TimePicker设定的时间就行了
mDatePicker.getCurrentHour();
mDatePicker.getCurrentMinute();
}
}).create();
timeDialog.show();
}
/**
* 方式三采取自定义view将弹出的样式设置成我们想要的
*/
MyPickerView hour;
MyPickerView minute;
private void dateDialog3() {
View view = this.getLayoutInflater().inflate(R.layout.mypicker_dialog,
null);
hour = (MyPickerView) view.findViewById(R.id.minute_pv);
minute = (MyPickerView) view.findViewById(R.id.second_pv);
//创建两个集合分别用来装长度 我们的MyPickerView继承view 没有 void onTimeChanged(TimePicker view, int hourOfDay, int minute);
List<String> data = new ArrayList<String>();
List<String> seconds = new ArrayList<String>();
for (int i = 0; i < 24; i++) {
data.add(i<10?"0" + i:""+i);//添加0
}
for (int i = 0; i < 60; i++) {
seconds.add(i < 10 ? "0" + i : "" + i);
}
hour.setData(data);
hour.setOnSelectListener(new onSelectListener() {
@Override
public void onSelect(String text) {
// TODO Auto-generated method stub
//获取分钟
}
});
minute.setData(seconds);
minute.setOnSelectListener(new onSelectListener() {
@Override
public void onSelect(String text) {
Toast.makeText(context, "选择了 " + text + " 分",
Toast.LENGTH_SHORT).show();
}
});
AlertDialog.Builder timeDialog = new AlertDialog.Builder(context);
timeDialog.setTitle("一座城池2012");
timeDialog.setIcon(null);
timeDialog.setCancelable(false);
/**
* 添加view
*/
timeDialog.setView(view);
timeDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 取出MyTimePicker设定的时间就行了
}
}).create();
timeDialog.show();
}
========================
}
=======================
MyPickerView :::
package com.example.onebelowzero;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MyPickerView extends View {
public static final String TAG = "PickerView";
/**
* text之间间距和minTextSize之比
*/
public static final float MARGIN_ALPHA = 2.8f;
/**
* 自动回滚到中间的速度
*/
public static final float SPEED = 2;
private List<String> mDataList;
/**
* 选中的位置,这个位置是mDataList的中心位置,一直不变
*/
private int mCurrentSelected;
private Paint mPaint;
private float mMaxTextSize = 80;
private float mMinTextSize = 40;
private float mMaxTextAlpha = 255;
private float mMinTextAlpha = 120;
private int mColorText = 0x333333;//文字颜色,随便设置
private int mViewHeight;
private int mViewWidth;
private float mLastDownY;
/**
* 滑动的距离
*/
private float mMoveLen = 0;
private boolean isInit = false;
private onSelectListener mSelectListener;
private Timer timer;
private MyTimerTask mTask;
Handler updateHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (Math.abs(mMoveLen) < SPEED) {
mMoveLen = 0;
if (mTask != null) {
mTask.cancel();
mTask = null;
performSelect();
}
} else
// 这里mMoveLen / Math.abs(mMoveLen)是为了保有mMoveLen的正负号,以实现上滚或下滚
mMoveLen = mMoveLen - mMoveLen / Math.abs(mMoveLen) * SPEED;
invalidate();
}
};
public MyPickerView(Context context) {
super(context);
init();
}
public MyPickerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setOnSelectListener(onSelectListener listener) {
mSelectListener = listener;
}
private void performSelect() {
if (mSelectListener != null)
mSelectListener.onSelect(mDataList.get(mCurrentSelected));
}
public void setData(List<String> datas) {
mDataList = datas;
mCurrentSelected = datas.size() / 2;
invalidate();
}
public void setSelected(int selected) {
mCurrentSelected = selected;
int distance = mDataList.size() / 2 - mCurrentSelected;
if (distance < 0)
for (int i = 0; i < -distance; i++)
{
moveHeadToTail();
mCurrentSelected--;
}
else if (distance > 0)
for (int i = 0; i < distance; i++)
{
moveTailToHead();
mCurrentSelected++;
}
invalidate();
}
private void moveHeadToTail() {
String head = mDataList.get(0);
mDataList.remove(0);
mDataList.add(head);
}
private void moveTailToHead() {
String tail = mDataList.get(mDataList.size() - 1);
mDataList.remove(mDataList.size() - 1);
mDataList.add(0, tail);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mViewHeight = getMeasuredHeight();
mViewWidth = getMeasuredWidth();
// 按照View的高度计算字体大小
mMaxTextSize = mViewHeight / 4.0f;
mMinTextSize = mMaxTextSize / 2f;
isInit = true;
invalidate();
}
private void init() {
timer = new Timer();
mDataList = new ArrayList<String>();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Style.FILL);
mPaint.setTextAlign(Align.CENTER);
mPaint.setColor(mColorText);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 根据index绘制view
if (isInit)
drawData(canvas);
}
private void drawData(Canvas canvas) {
// 先绘制选中的text再往上往下绘制其余的text
float scale = parabola(mViewHeight / 4.0f, mMoveLen);
float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize;
mPaint.setTextSize(size);
mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha));
// text居中绘制,注意baseline的计算才能达到居中,y值是text中心坐标
float x = (float) (mViewWidth / 2.0);
float y = (float) (mViewHeight / 2.0 + mMoveLen);
FontMetricsInt fmi = mPaint.getFontMetricsInt();
float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0));
canvas.drawText(mDataList.get(mCurrentSelected), x, baseline, mPaint);
// 绘制上方data
for (int i = 1; (mCurrentSelected - i) >= 0; i++) {
drawOtherText(canvas, i, -1);
}
// 绘制下方data
for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) {
drawOtherText(canvas, i, 1);
}
}
/**
* @param canvas
* @param position
* 距离mCurrentSelected的差值
* @param type
* 1表示向下绘制,-1表示向上绘制
*/
private void drawOtherText(Canvas canvas, int position, int type) {
float d = (float) (MARGIN_ALPHA * mMinTextSize * position + type
* mMoveLen);
float scale = parabola(mViewHeight / 4.0f, d);
float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize;
mPaint.setTextSize(size);
mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha));
float y = (float) (mViewHeight / 2.0 + type * d);
FontMetricsInt fmi = mPaint.getFontMetricsInt();
float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0));
canvas.drawText(mDataList.get(mCurrentSelected + type * position),
(float) (mViewWidth / 2.0), baseline, mPaint);
}
/**
* 抛物线
*
* @param zero
* 零点坐标
* @param x
* 偏移量
* @return scale
*/
private float parabola(float zero, float x) {
float f = (float) (1 - Math.pow(x / zero, 2));
return f < 0 ? 0 : f;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
doDown(event);
break;
case MotionEvent.ACTION_MOVE:
doMove(event);
break;
case MotionEvent.ACTION_UP:
doUp(event);
break;
}
return true;
}
private void doDown(MotionEvent event) {
if (mTask != null) {
mTask.cancel();
mTask = null;
}
mLastDownY = event.getY();
}
private void doMove(MotionEvent event) {
mMoveLen += (event.getY() - mLastDownY);
if (mMoveLen > MARGIN_ALPHA * mMinTextSize / 2) {
// 往下滑超过离开距离
moveTailToHead();
mMoveLen = mMoveLen - MARGIN_ALPHA * mMinTextSize;
} else if (mMoveLen < -MARGIN_ALPHA * mMinTextSize / 2) {
// 往上滑超过离开距离
moveHeadToTail();
mMoveLen = mMoveLen + MARGIN_ALPHA * mMinTextSize;
}
mLastDownY = event.getY();
invalidate();
}
private void doUp(MotionEvent event) {
// 抬起手后mCurrentSelected的位置由当前位置move到中间选中位置
if (Math.abs(mMoveLen) < 0.0001) {
mMoveLen = 0;
return;
}
if (mTask != null) {
mTask.cancel();
mTask = null;
}
mTask = new MyTimerTask(updateHandler);
timer.schedule(mTask, 0, 10);
}
class MyTimerTask extends TimerTask {
Handler handler;
public MyTimerTask(Handler handler) {
this.handler = handler;
}
@Override
public void run() {
handler.sendMessage(handler.obtainMessage());
}
}
/**
* 定义接口用来选择
* @author kcw001
*/
public interface onSelectListener {
void onSelect(String text);
}
}
布局文件等不贴了,见完整代码:
http://download.csdn.net/detail/onebelowzero2012/9374740
TimePicker,TimePickerDialog以及自定义timepicker(一)的更多相关文章
- Android中DatePicker与TimePicker用法讲解(包括DatePickerDialog与TimePickerDialog)
实现效果:将DatePicker和TimePicker修改的日期和时间实时显示在程序标题栏上. 1.通过DatePicker和TimePicker来实现 布局为main.xml <?xml ve ...
- Android 自定义TimePickerDialog
学习Android半年了,一直想写点东西,今天开始写第一篇,目前在弄的一个小项目,是关于课程表智能闹钟,今天做这个TimePickerDialog查了很多资料,做成了下面这个效果, 这里timepic ...
- android 开发 时间选择器TimePicker的使用
android系统自带时间控件:DatePicker 日期显示控件 DatePickerDialog 日期对话框控件TimePicker 时间显示控件 TimePickerDialog 时间对话框控件 ...
- android中TimePicker和DatePicker的简单使用
package com.example.demo10; import java.util.Calendar; import android.support.v7.app.ActionBarActivi ...
- TimePicker 和TimePickerDiag
先建立以个xml,然后拖拽TimerPicker; TimePicker和TimePickerDiag TimerPicker : 先建立xml文件,然后拖拽TimePicker然后在MainActi ...
- 安卓开发_浅谈TimePicker(时间选择器)
TimePicker也继承自FrameLayout类.时间选择控件向用户显示一天中的时间(可以为24小时,也可以为AM/PM制),并允许用户进行选择.如果要捕获用户修改时间数据的事件,便需要为Time ...
- Android 使用DatePicker以及TimePicker显示当前日期和时间
课程内容1.介绍DatePicker和TimePicker两种实现动态输入日期和事件的功能2.介绍DatePickerDialog和TimePickerDialog来年耕种实现动态输入日期和事件的对话 ...
- Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker
原文:Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker 在实际开发中,经常会遇见一些时间选择器.日期选择器.数字选择器等需求,那么从本期开始来学习And ...
- jQuery Dialog and timepicker显示层的问题
timepicker官网http://timepicker.co/demos/ 当在dialogue上面调用时间选择时,时间选择的框框被dialogue窗口挡住了. 搜出来的方法说修改css,根本改不 ...
随机推荐
- .NET生成PDF文件
C#未借助第三方组件,自己封装通用类,生成PDF文件. 调用方式: //路径 string path = @"C:\yuannwu22.pdf"; //内容 string strC ...
- 常见sql语句及复杂sql语句记录
1 将A表中的部分字段和B表中的部分字段作为C表的字段 SELECT a.name,b.age FROM (SELECT * from A) as a,(SELECT * from B) as ...
- SqlSugar框架T4模板的使用
一.T4模板说明 1.T4模板是用来生成Model层实体类的 2.文件后缀为.tt 3.需要修改配置主要有:引用的SqlSugar.dll的位置.生成实体类的位置及生成实体类的命名空间 4.T4模板生 ...
- [JS] JavascriptHelp (转载)
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- VS编译出现 HTTP 错误 403.14 - Forbidden 决绝办法
决绝办法: 运行cmd命令,在控制台面板计入Iis Express目录下.运行提示的的就可以了 appcmd set config /section:system.webServe ...
- MyEclipse激活失败,解决办法
文章参考:http://www.cnblogs.com/dingyuanxin/p/4046356.html 失败可能是:systemid和exe破解出来的那个对应不上: 1.启动MyEclipse, ...
- 2014-11-26----css的简介
CSS :层叠样式表 cascading style sheets 它的作用是:美化html网页 格式:样式名:值:样式名:值:样式名:值: 注释语法:/* 注释内容 */ 选中代码按TAB,代码左移 ...
- 视频处理简单实例 [OpenCV 笔记2]
VideoCapture是OpenCV 2.X中新增的类,提供从摄像机或视频文件捕获视频的C++接口.利用它读入视频的方法一般有两种: // method 1 VideoCapture capture ...
- CentOS 7.0 重置root密码
步骤一,开机时随便按下键盘,进入以下菜单 步骤二: 选择第一项,按e键进行修改 步骤三,定位到 ro( linux 16 or linuxefi ) 步骤四:把ro改成 “rw init=/sysro ...
- [Neural Networks] Momentum
一.目的 加快参数的收敛速度. 二.做法 另第t次的权重更新对第t+1次的权重更新造成影响. 从上式可看出,加入momentum后能够保持权重的更新方向,同时加快收敛.通常alpha的取值为[0.7, ...