本文Github代码链接
https://github.com/AndroidMsky/AndoirdIOSPicker
先上图吧:
这是笔者最近一个项目一直再用的一个选择器库,自己也在其中做了修改,并决定持续维护下去。
先看使用方法:
日期选择:
private void showDateDialog(List<Integer> date) { DatePickerDialog.Builder builder = new DatePickerDialog.Builder(this); builder.setOnDateSelectedListener(new DatePickerDialog.OnDateSelectedListener() { @Override public void onDateSelected(int[] dates) { mTextView.setText(dates[0] + "-" + (dates[1] > 9 ? dates[1] : ("0" + dates[1])) + "-" + (dates[2] > 9 ? dates[2] : ("0" + dates[2]))); } @Override public void onCancel() { } }) .setMinYear(1900) .setMaxYear(2050) .setSelectYear(date.get(0) - 1) .setSelectMonth(date.get(1) - 1) .setSelectDay(date.get(2) - 1); builder.setMaxYear(DateUtil.getYear()); builder.setMaxMonth(DateUtil.getDateForString(DateUtil.getToday()).get(1)); builder.setMaxDay(DateUtil.getDateForString(DateUtil.getToday()).get(2)); dateDialog = builder.create(); dateDialog.show(); }
比较简单就不解释了
自定义选择:
先搞一个list
private List<String> list = new ArrayList<>();
然后调用时候传入这个list就可以了
/** * chooseDialog */ private void showChooseDialog(List<String> mlist) { DataPickerDialog.Builder builder = new DataPickerDialog.Builder(this); chooseDialog = builder.setData(mlist).setSelection(1).setTitle("取消") .setOnDataSelectedListener(new DataPickerDialog.OnDataSelectedListener() { @Override public void onDataSelected(String itemValue, int position) { mTextView.setText(itemValue); } @Override public void onCancel() { } }).create(); chooseDialog.show(); }
接下来我们就那timepick开刀简单分析下其中的原理,也方便我们做自定义的扩展。
首先打开TimePickerDialog可见继承自Dialog对自定义Dialog还不熟悉的可以看:
安卓下Builder模式解析+自定义Dialog实战演练
http://blog.csdn.net/androidmsky/article/details/52982815
public class TimePickerDialog extends Dialog
肯定这中Dialog都会使用Builder模式,接下来看里面的字段
private static final class Params { private boolean shadow = true; private boolean canCancel = true; private LoopView loopHour, loopMin; private OnTimeSelectedListener callback; }
看到主力军是两个LoopView来表示小时和分钟,接下来我们就要看LoopView这类了,进去会发现比较庞大有一脸的参数。不用怕,我们直接来到它的两个最关键的方法,
protected void onDraw(Canvas canvas)
可以看到就是在把文字画出来也不要怕反反复复就那么几个方法:
核心就是它 canvas.drawText(as[j1], startX, h, paintB);
在几种情况下调用它,肯定就是12345个位置数字不同的样式
if (i2 <= n && h + i2 >= n) { canvas.save(); canvas.clipRect(0, 0, v, n - i2); canvas.drawText(as[j1], startX, h, paintA); canvas.restore(); canvas.save(); canvas.clipRect(0, n - i2, v, (int) ((float) h * l)); canvas.drawText(as[j1], startX, h, paintB); canvas.restore(); } else if (i2 <= o && h + i2 >= o) { canvas.save(); canvas.clipRect(0, 0, v, o - i2); canvas.drawText(as[j1], startX, h, paintB); canvas.restore(); canvas.save(); canvas.clipRect(0, o - i2, v, (int) ((float) h * l)); canvas.drawText(as[j1], startX, h, paintA); canvas.restore(); } else if (i2 >= n && h + i2 <= o) { canvas.clipRect(0, 0, v, (int) ((float) h * l)); canvas.drawText(as[j1], startX, h, paintB); mSelectItem = arrayList.indexOf(as[j1]); } else { canvas.clipRect(0, 0, v, (int) ((float) h * l)); canvas.drawText(as[j1], startX, h, paintA); } canvas.restore();
下一个关键方法就是:
public boolean onTouchEvent(MotionEvent motionevent)
通过手指的移动改变绘制的偏移值:
case MotionEvent.ACTION_MOVE: y = motionevent.getRawY(); z = x - y; x = y; totalScrollY = (int) ((float) totalScrollY + z); if (!isLoop) { if (totalScrollY > (int) ((float) (-positon) * (l * (float) h))) { break; /* Loop/switch isn't completed */ } totalScrollY = (int) ((float) (-positon) * (l * (float) h)); } break;
大概就是这种姿势去看开源自定义view了。
以上所述是小编给大家介绍的Android开发中实现IOS风格底部选择器(支持时间 日期 自定义),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对找一找教程网网站的支持!