安卓的api中提供了DatePickerDailog、TimePickerDialog,这种都是比较复杂的数值型选择器。但纯数字选择器Dialog却未提供。
DataPicker、TimePicker都是FrameLayout,而本例使用的NumberPicker却是LinearLayout,具体未深入研究。总之,看起来前2者更亲近,我们要实现的NumberPickerDialog相貌和前2者差不多。
这里的代码也是依照TimePickerDialog仿写。
效果图:
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
/* * Copyright (C) cuiweiyou.com/崔维友 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.cuiweiyou.view; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.NumberPicker; import com.cuiweiyou.R; /** * A dialog that prompts the user for the number using a NumberPicker.<br/> * 使用NumberPicker获取数值的对话框 */ public class NumberPickerDialog extends AlertDialog implements OnClickListener, NumberPicker.OnValueChangeListener { private final String maxValue = "最大值"; private final String minValue = "最小值"; private final String currentValue = "当前值"; private final NumberPicker mNumberPicker; private final NumberPicker.OnValueChangeListener mCallback; private int newVal; private int oldVal; /** * @param context 上下文 * @param callBack 回调器 * @param maxValueNumber 最大值 * @param minValueNumber 最小值 * @param currentValueNumber 当前值 */ public NumberPickerDialog(Context context, NumberPicker.OnValueChangeListener callBack, int maxValueNumber, int minValueNumber, int currentValueNumber) { super(context, 0); mCallback = callBack; setIcon(0); setTitle("设置数字"); Context themeContext = getContext(); setButton(BUTTON_POSITIVE, "设置", this); setButton(BUTTON_NEGATIVE, "取消", this); LayoutInflater inflater = (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.view_number_picker_dialog, null); setView(view); mNumberPicker = (NumberPicker) view.findViewById(R.id.numberPicker); mNumberPicker.setMaxValue(maxValueNumber); mNumberPicker.setMinValue(minValueNumber); mNumberPicker.setValue(currentValueNumber); mNumberPicker.setOnValueChangedListener(this); } @Override public Bundle onSaveInstanceState() { Bundle state = super.onSaveInstanceState(); state.putInt(maxValue, mNumberPicker.getMaxValue()); state.putInt(minValue, mNumberPicker.getMinValue()); state.putInt(currentValue, mNumberPicker.getValue()); return state; } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); int max = savedInstanceState.getInt(maxValue); int min = savedInstanceState.getInt(minValue); int cur = savedInstanceState.getInt(currentValue); mNumberPicker.setMaxValue(max); mNumberPicker.setMinValue(min); mNumberPicker.setValue(cur); } @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { this.oldVal = oldVal; this.newVal = newVal; } @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case BUTTON_POSITIVE: mCallback.onValueChange(mNumberPicker, oldVal, newVal); break; } } /** * <b>功能</b>: setCurrentValue,设置NumberPicker的当前值<br/> * @author : weiyou.com <br/> * @return */ public NumberPickerDialog setCurrentValue(int value){ mNumberPicker.setValue(value); return this; } } |
这个view_number_picker_dialog.xml布局相当简单,其中仅注册一个NumberPicker:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3.5" android:gravity="center" android:orientation="horizontal" > <NumberPicker android:id="@+id/numberPicker" android:layout_width="wrap_content" android:descendantFocusability="blocksDescendants" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> |
使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
new NumberPickerDialog( this, new OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { Log.e("ard", "所选值:" + picker.getValue() + ",原值:" + oldVal + ",新值:" + newVal); // 新值即所选值 } }, 90, // 最大值 20, // 最小值 40) // 默认值 .setCurrentValue(55) // 更新默认值 .show(); |
整体非常easy,没什么复杂的功能。
看了下NumberPicker的源码,很,长。
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/1824.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设