相信很多Android程序员在开发过程中会碰到会做时间选择器,并且有时间会与和iOS时间选择器长的差不多,下面是本人写的用Popwindow结合NumberPicker写的时间选择器,能力有限,如有建议和意见请与本人联系QQ2428566234,也可以到QQ技术讨论群群号(群号387648673)讨论问题
本人写的是选择年月,直接上代码
Popwindow布局datepick.xml
<?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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<TextView
android:id="@+id/datepicker_cancle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:textSize="18sp" />
<TextView
android:id="@+id/datepicker_sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="确定"
android:textColor="#f00"
android:textSize="18sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<NumberPicker
android:id="@+id/datepicker_year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:clickable="false" >
</NumberPicker>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<NumberPicker
android:id="@+id/datepicker_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:clickable="false" >
</NumberPicker>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Activity代码 在chooseDateRl点击 显示到chooseDateTv;
private RelativeLayout chooseDateRl;
private TextView chooseDateTv;
在chooseDateRl的点击事件里showPopupWindow();
下面是PopupWindow 方法 现在选择是从2008年6月到现在时间的年月的选择
private void showPopupWindow(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.datepick, null);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
//设置popWindow可点击
popupWindow.setTouchable(true);
//为popWindow设置背景
ColorDrawable cd = new ColorDrawable(Color.parseColor("#FFFFFFFF"));
popupWindow.setBackgroundDrawable(cd);
popupWindow.showAtLocation(chooseDateRl, Gravity.BOTTOM,0, 0);
yearPicker = (NumberPicker) view.findViewById(R.id.datepicker_year);
monthPicker = (NumberPicker) view.findViewById(R.id.datepicker_month);
surePickerTv = (TextView) view.findViewById(R.id.datepicker_sure);
canclePickerTv = (TextView)view.findViewById(R.id.datepicker_cancle);
Calendar now = Calendar.getInstance();
final int minYear = 2008;
yearPicker.setMinValue(minYear);
final int maxYear = now.get(Calendar.YEAR);
yearPicker.setMaxValue(maxYear);
monthPicker.setMinValue(1);
monthPicker.setMaxValue(12);
final int nowMonth = now.get(Calendar.MONTH)+1;
yearPicker.setFocusable(false);
yearPicker.setValue(minYear);
monthPicker.setValue(0);
//这个方法比较重要,是阻止用户点击NumberPicker出来键盘
yearPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
monthPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
//NumberPicker的滑动监听事件
yearPicker.setOnValueChangedListener(new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// TODO Auto-generated method stub
if(newVal==maxYear){
monthPicker.setMaxValue(nowMonth);
}else {
monthPicker.setMaxValue(12);
}
if (newVal == minYear) {
monthPicker.setMinValue(6);
}else {
monthPicker.setMinValue(1);
}
}
});
monthPicker.setOnValueChangedListener(new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// TODO Auto-generated method stub
}
});
surePickerTv.setOnClickListener(this);
canclePickerTv.setOnClickListener(this);
}
在点击事件里,将选择的年月赋值到chooseDateTv
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.rl_main_chooseDate:
showPopupWindow();
break;
case R.id.datepicker_sure:
int year = yearPicker.getValue();
int month = monthPicker.getValue();
chooseDateTv.setText(year+"年"+month+"月");
popupWindow.dismiss();
break;
case R.id.datepicker_cancle:
popupWindow.dismiss();
chooseDateTv.setText("请选择");
SharedPreferencesUtil.putString(MainActivity.this, "date", ("请选择"));
break;
default:
break;
}
}