1 效果图
2 NumberPicker
- numberPicker 可以指定最大、最小值,有一个int型的value来记录向上向下的值
- 可以显示字符串,只需要指定依赖的String[]
setDisplayedValues - 字体颜色、大小、分割线高度和颜色需要通过反射或者重写方法来设置
- 默认背景显示安卓系统,此时可通过设置Activity的Theme来覆盖
3 简单使用
TextNumberPicker
public class TextNumberPicker extends NumberPicker {
public TextNumberPicker(Context context) {
super(context);
}
public TextNumberPicker(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextNumberPicker(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
updateView(child);
}
private void updateView(View child) {
if (child instanceof EditText) {
((EditText) child).setTextSize(24);
}
}
public static void setDividerColor(NumberPicker picker, ColorDrawable color) {
Field field = null;
try {
field = NumberPicker.class.getDeclaredField("mSelectionDivider");
if (field != null) {
field.setAccessible(true);
field.set(picker, color);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setDividerHeight(NumberPicker picker, int h) {
Field[] fields = NumberPicker.class.getDeclaredFields();
for (Field field : fields) {
if (field.getName().equals("mSelectionDividerHeight")) {
field.setAccessible(true);
try {
field.set(picker, h);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
}
封装了PopuWindow弹框类AutomaticPp ,
注意主要属性设置:initPicker()
public class AutomaticPp {
private PopupWindow popupWindow;
private TextNumberPicker tp1, tp2;
public static final String[] TAB1 = {"每月", "每周", "每双周"};
public static final String[] TAB_WEEK = {"周一", "周二", "周三", "周四", "周五"};
public static final int MONTH_MAX = 28;
public AutomaticPp(final Context context) {
View pop = View.inflate(context, R.layout.pp_financial_auto, null);
popupWindow = new PopupWindow(pop, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable(context.getResources(), (Bitmap) null));
tp1 = pop.findViewById(R.id.tp1);
tp1.setDisplayedValues(TAB1);
tp1.setMaxValue(TAB1.length - 1);
/* tp1.setFormatter(new NumberPicker.Formatter() {
@Override
public String format(int i) {
return TAB1[i];
}
});*/
initPicker(tp1, 0);
tp2 = pop.findViewById(R.id.tp2);
tp2.setMaxValue(MONTH_MAX);
initPicker(tp2, 1);
tp1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
if (newVal == 0) {
tp2.setMinValue(1);
tp2.setMaxValue(28);
tp2.setDisplayedValues(null);
tp2.setValue(1);
} else {
tp2.setMinValue(0);
tp2.setMaxValue(TAB_WEEK.length - 1);
tp2.setDisplayedValues(TAB_WEEK);
if (oldVal == 0) {
tp2.setValue(0);
}
}
}
});
pop.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
pop.findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "tp1 =" + tp1.getValue() + " tp2 =" + tp2.getValue(), Toast.LENGTH_LONG).show();
popupWindow.dismiss();
}
});
}
private void initPicker(TextNumberPicker picker, int min) {
picker.setMinValue(min);
picker.setValue(min);
//不可编辑 picker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
//不循环滚动
picker.setWrapSelectorWheel(false);
//分割线颜色
TextNumberPicker.setDividerColor(picker, new ColorDrawable(Color.parseColor("#ADB0A7")));
TextNumberPicker.setDividerHeight(picker, UtilsDensity.dip2px(0.5f));
}
public void showAtLocation(View view, int bottom, int x, int y) {
popupWindow.showAtLocation(view, bottom, x, y);
}
public void dismiss() {
popupWindow.dismiss();
}
public boolean isShowing() {
return popupWindow.isShowing();
}
}
4 Demo
5 注意
1.第一次加载不建议用setFormatter,首次会不显示;推荐setDisplayedValues
2. 当setDisplayedValues切换不同的String数组时,由于两个数组大小不同 注意setDisplayedValues 和setMaxValue的顺序
//注意代码执行顺序,此时String数组 st1的长度 小于 st2
if (newVal == 0) {
tp.setMaxValue(st1.length - 1);
tp.setDisplayedValues(st1);
} else {
tp.setDisplayedValues(st2);
tp.setMaxValue(st2.length - 1);
}
3 设置Theme覆盖NumberPicker自带的系统UI
values.xml定义,具体theme根据项目情况
<style name="Theme.picker" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:editTextStyle">@style/Widget.EditText.White</item>
</style>
<style name="Widget.EditText.White" parent="@android:style/Widget.EditText">
AndroidManifest.xml声明
<activity
android:name=".nineth.StringPickerActivity"
android:exported="false"
android:screenOrientation="portrait"
android:theme="@style/Theme.picker"
android:windowSoftInputMode="adjustResize" />