From a676b4fa1949d8146bc8d921d6b6cf9eb89bfdd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E9=80=B8?= Date: Thu, 5 Jan 2017 16:12:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=80=89=E6=8B=A9=E6=97=A5?= =?UTF-8?q?=E6=9C=9FDialog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dialogWheel/DialogWheelYearMonthDay.java | 436 ++++++++++-------- .../main/res/layout/dialog_year_month_day.xml | 49 +- .../tools/activity/ActivityDialog.java | 18 +- 3 files changed, 283 insertions(+), 220 deletions(-) diff --git a/VonTools/src/main/java/com/vondear/vontools/view/dialogWheel/DialogWheelYearMonthDay.java b/VonTools/src/main/java/com/vondear/vontools/view/dialogWheel/DialogWheelYearMonthDay.java index 1d7f3ac6..5b55c7a1 100644 --- a/VonTools/src/main/java/com/vondear/vontools/view/dialogWheel/DialogWheelYearMonthDay.java +++ b/VonTools/src/main/java/com/vondear/vontools/view/dialogWheel/DialogWheelYearMonthDay.java @@ -20,194 +20,252 @@ import java.util.Calendar; public class DialogWheelYearMonthDay extends TransparentDialog { - private Activity context; - private WheelView year; - private WheelView month; - private WheelView day; - private int curYear; - private int curMonth; - private int curDay; - private TextView tv_sure; - private TextView tv_cancle; - private CheckBox checkBox_day; - private Calendar calendar; - private String months[] = new String[] { "01", "02", "03", "04", "05", - "06", "07", "08", "09", "10", "11", "12" }; - public DialogWheelYearMonthDay(Activity context) { - super(context); - // TODO Auto-generated constructor stub - this.context = context; - build(); - } - - public DialogWheelYearMonthDay(Activity context, TextView tv_time) { - super(context); - // TODO Auto-generated constructor stub - this.context = context; - build(); - tv_time.setText(curYear+"年"+months[curMonth]+"月"); - } - - private void build(){ - calendar = Calendar.getInstance(); - final View dialogView1 = LayoutInflater.from( - context).inflate( - R.layout.dialog_year_month_day, null); - - OnWheelChangedListener listener = new OnWheelChangedListener() { - @Override - public void onChanged(WheelView wheel, int oldValue, int newValue) { - updateDays(year, month,day); - } - }; - - //year - year = (WheelView) dialogView1 - .findViewById(R.id.wheelView_year); - year.setBackgroundResource(R.drawable.transparent_bg); - year.setWheelBackground(R.drawable.transparent_bg); - year.setWheelForeground(R.drawable.wheel_val_holo); - year.setShadowColor(0xFFDADCDB, 0x88DADCDB, 0x00DADCDB); - curYear = calendar.get(Calendar.YEAR); - year.setViewAdapter(new DateNumericAdapter(context, curYear-5, curYear,5)); - year.setCurrentItem(5); - year.addChangingListener(listener); - - - // month - month = (WheelView) dialogView1 - .findViewById(R.id.wheelView_month); - month.setBackgroundResource(R.drawable.transparent_bg); - month.setWheelBackground(R.drawable.transparent_bg); - month.setWheelForeground(R.drawable.wheel_val_holo); - month.setShadowColor(0xFFDADCDB, 0x88DADCDB, 0x00DADCDB); - curMonth = calendar.get(Calendar.MONTH); - month.setViewAdapter(new DateArrayAdapter(context, months, curMonth)); - month.setCurrentItem(curMonth); - month.addChangingListener(listener); - - - //day - day = (WheelView) dialogView1.findViewById(R.id.wheelView_day); - updateDays(year, month, day); - curDay = calendar.get(Calendar.DAY_OF_MONTH); - day.setCurrentItem(curDay-1); - day.setBackgroundResource(R.drawable.transparent_bg); - day.setWheelBackground(R.drawable.transparent_bg); - day.setWheelForeground(R.drawable.wheel_val_holo); - day.setShadowColor(0xFFDADCDB, 0x88DADCDB, 0x00DADCDB); - - tv_sure = (TextView) dialogView1.findViewById(R.id.tv_sure); - tv_cancle = (TextView) dialogView1 - .findViewById(R.id.tv_cancle); - - checkBox_day = (CheckBox) dialogView1.findViewById(R.id.checkBox_day); - checkBox_day.setOnCheckedChangeListener(new OnCheckedChangeListener() { - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - // TODO Auto-generated method stub - if (isChecked) { - day.setVisibility(View.VISIBLE); - }else{ - day.setVisibility(View.GONE); - } - } - }); - - getAttr().gravity = Gravity.CENTER; - setContentView(dialogView1); - } - - - public WheelView getDay() { - return day; - } - - public int getCurDay() { - return curDay; - } - - public CheckBox getCheckBox_day() { - return checkBox_day; - } - - public WheelView getYear() { - return year; - } - - public WheelView getMonth() { - return month; - } - - public int getCurYear() { - return curYear; - } - - public int getCurMonth() { - return curMonth; - } - - public TextView getTv_sure() { - return tv_sure; - } - - public TextView getTv_cancle() { - return tv_cancle; - } - - public String[] getMonths() { - return months; - } - - - /** - * Updates day wheel. Sets max days according to selected month and year - */ - private void updateDays(WheelView year, WheelView month, WheelView day) { - calendar.set(Calendar.YEAR, curYear-5 + year.getCurrentItem()); - calendar.set(Calendar.MONTH, month.getCurrentItem()); - int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); - int maxDays = VonDataUtils.getDaysByYearMonth(curYear-5 + year.getCurrentItem(), month.getCurrentItem()+1); - day.setViewAdapter(new DateNumericAdapter(context, 1, maxDays, calendar.get(Calendar.DAY_OF_MONTH) - 1)); - int curDay = Math.min(maxDays, day.getCurrentItem() + 1); - day.setCurrentItem(curDay - 1, true); - - - - } - - /** - * Adapter for numeric wheels. Highlights the current value. - */ - private class DateNumericAdapter extends NumericWheelAdapter { - // Index of current item - int currentItem; - // Index of item to be highlighted - int currentValue; - - /** - * Constructor - */ - public DateNumericAdapter(Context context, int minValue, int maxValue, int current) { - super(context, minValue, maxValue); - this.currentValue = current; - setTextSize(16); - } - - @Override - protected void configureTextView(TextView view) { - super.configureTextView(view); - /*if (currentItem == currentValue) { - view.setTextColor(0xFF0000F0); + private Context context; + private WheelView year; + private WheelView month; + private WheelView day; + private int curYear; + private int curMonth; + private int curDay; + private TextView tv_sure; + private TextView tv_cancle; + private CheckBox checkBox_day; + private Calendar calendar; + private String months[] = new String[]{"01", "02", "03", + "04", "05", "06", "07", "08", "09", "10", "11", "12"}; + private String days[] = new String[]{"01", "02", "03", "04", "05", "06", "07", + "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", + "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}; + + private int beginYear = 0; + private int endYear = 0; + private int divideYear = endYear - beginYear; + + public int getBeginYear() { + return beginYear; + } + + public int getEndYear() { + return endYear; + } + + public int getDivideYear() { + return divideYear; + } + + public DialogWheelYearMonthDay(Context context) { + super(context); + // TODO Auto-generated constructor stub + this.context = context; + build(); + } + + public DialogWheelYearMonthDay(Context context, int beginYear) { + super(context); + // TODO Auto-generated constructor stub + this.context = context; + this.beginYear = beginYear; + build(); + } + + public DialogWheelYearMonthDay(Context context, int beginYear, int endYear) { + super(context); + // TODO Auto-generated constructor stub + this.context = context; + this.beginYear = beginYear; + this.endYear = endYear; + build(); + } + + public DialogWheelYearMonthDay(Context context, TextView tv_time) { + super(context); + // TODO Auto-generated constructor stub + this.context = context; + build(); + tv_time.setText(curYear + "年" + months[curMonth] + "月"); + } + + private void build() { + calendar = Calendar.getInstance(); + final View dialogView1 = LayoutInflater.from(context).inflate(R.layout.dialog_year_month_day, null); + + OnWheelChangedListener listener = new OnWheelChangedListener() { + @Override + public void onChanged(WheelView wheel, int oldValue, int newValue) { + updateDays(year, month, day); + } + }; + + curYear = calendar.get(Calendar.YEAR); + if (beginYear == 0) { + beginYear = curYear - 5; + } + if (endYear == 0) { + endYear = curYear; + } + if (beginYear > endYear) { + endYear = beginYear; + } + + //year + year = (WheelView) dialogView1 + .findViewById(R.id.wheelView_year); + year.setBackgroundResource(R.drawable.transparent_bg); + year.setWheelBackground(R.drawable.transparent_bg); + year.setWheelForeground(R.drawable.wheel_val_holo); + year.setShadowColor(0xFFDADCDB, 0x88DADCDB, 0x00DADCDB); + year.setViewAdapter(new DateNumericAdapter(context, beginYear, endYear, endYear - beginYear)); + year.setCurrentItem(endYear - beginYear); + year.addChangingListener(listener); + + + // month + month = (WheelView) dialogView1 + .findViewById(R.id.wheelView_month); + month.setBackgroundResource(R.drawable.transparent_bg); + month.setWheelBackground(R.drawable.transparent_bg); + month.setWheelForeground(R.drawable.wheel_val_holo); + month.setShadowColor(0xFFDADCDB, 0x88DADCDB, 0x00DADCDB); + curMonth = calendar.get(Calendar.MONTH); + month.setViewAdapter(new DateArrayAdapter(context, months, curMonth)); + month.setCurrentItem(curMonth); + month.addChangingListener(listener); + + + //day + day = (WheelView) dialogView1.findViewById(R.id.wheelView_day); + updateDays(year, month, day); + curDay = calendar.get(Calendar.DAY_OF_MONTH); + day.setCurrentItem(curDay - 1); + day.setBackgroundResource(R.drawable.transparent_bg); + day.setWheelBackground(R.drawable.transparent_bg); + day.setWheelForeground(R.drawable.wheel_val_holo); + day.setShadowColor(0xFFDADCDB, 0x88DADCDB, 0x00DADCDB); + + tv_sure = (TextView) dialogView1.findViewById(R.id.tv_sure); + tv_cancle = (TextView) dialogView1.findViewById(R.id.tv_cancle); + + checkBox_day = (CheckBox) dialogView1.findViewById(R.id.checkBox_day); + checkBox_day.setOnCheckedChangeListener(new OnCheckedChangeListener() { + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + // TODO Auto-generated method stub + if (isChecked) { + day.setVisibility(View.VISIBLE); + } else { + day.setVisibility(View.GONE); + } + } + }); + + getAttr().gravity = Gravity.CENTER; + setContentView(dialogView1); + } + + + public WheelView getDay() { + return day; + } + + public int getCurDay() { + return curDay; + } + + public CheckBox getCheckBox_day() { + return checkBox_day; + } + + public WheelView getYear() { + return year; + } + + public WheelView getMonth() { + return month; + } + + private int getCurYear() { + return curYear; + } + + private int getCurMonth() { + return curMonth; + } + + public TextView getTv_sure() { + return tv_sure; + } + + public TextView getTv_cancle() { + return tv_cancle; + } + + private String[] getMonths() { + return months; + } + + private String[] getDays() { + return days; + } + + public int getSelectorYear() { + return beginYear + getYear().getCurrentItem(); + } + + public String getSelectorMonth() { + return getMonths()[getMonth().getCurrentItem()]; + } + + public String getSelectorDay() { + return getDays()[getDay().getCurrentItem()]; + } + + /** + * Updates day wheel. Sets max days according to selected month and year + */ + private void updateDays(WheelView year, WheelView month, WheelView day) { + calendar.set(Calendar.YEAR, beginYear + year.getCurrentItem()); + calendar.set(Calendar.MONTH, month.getCurrentItem()); + int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + int maxDays = VonDataUtils.getDaysByYearMonth(beginYear + year.getCurrentItem(), month.getCurrentItem() + 1); + day.setViewAdapter(new DateNumericAdapter(context, 1, maxDays, calendar.get(Calendar.DAY_OF_MONTH) - 1)); + int curDay = Math.min(maxDays, day.getCurrentItem() + 1); + day.setCurrentItem(curDay - 1, true); + + + } + + /** + * Adapter for numeric wheels. Highlights the current value. + */ + private class DateNumericAdapter extends NumericWheelAdapter { + // Index of current item + int currentItem; + // Index of item to be highlighted + int currentValue; + + /** + * Constructor + */ + public DateNumericAdapter(Context context, int minValue, int maxValue, int current) { + super(context, minValue, maxValue); + this.currentValue = current; + setTextSize(16); + } + + @Override + protected void configureTextView(TextView view) { + super.configureTextView(view); + /*if (currentItem == currentValue) { + view.setTextColor(0xFF0000F0); }*/ - view.setTypeface(Typeface.SANS_SERIF); - } - - @Override - public View getItem(int index, View cachedView, ViewGroup parent) { - currentItem = index; - return super.getItem(index, cachedView, parent); - } - } + view.setTypeface(Typeface.SANS_SERIF); + } + + @Override + public View getItem(int index, View cachedView, ViewGroup parent) { + currentItem = index; + return super.getItem(index, cachedView, parent); + } + } } diff --git a/VonTools/src/main/res/layout/dialog_year_month_day.xml b/VonTools/src/main/res/layout/dialog_year_month_day.xml index 77810ba6..b501f2e9 100644 --- a/VonTools/src/main/res/layout/dialog_year_month_day.xml +++ b/VonTools/src/main/res/layout/dialog_year_month_day.xml @@ -1,14 +1,13 @@ + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="@drawable/shape_gray_circle_bg" + android:orientation="vertical"> + android:layout_height="60dp"> + android:textSize="20sp"/> + android:gravity="center" + android:orientation="vertical"> - + android:button="@drawable/selector_checkbox_sure_cancle" + android:checked="true"/> + + android:text="显示日期" + android:textColor="@color/_8c" + android:textSize="8dp"/> + android:background="#005D9E"/> + android:gravity="center"> + android:layout_height="80dp"/> + android:layout_marginLeft="20dp"/> + android:layout_marginLeft="20dp"/> + android:orientation="horizontal"> + android:textSize="18sp"/> + android:layout_marginBottom="10dp" + android:layout_marginTop="10dp" + android:background="#005D9E"/> + android:textSize="18sp"/> \ No newline at end of file diff --git a/app/src/main/java/com/vondear/tools/activity/ActivityDialog.java b/app/src/main/java/com/vondear/tools/activity/ActivityDialog.java index c0d75ba2..390f6afe 100644 --- a/app/src/main/java/com/vondear/tools/activity/ActivityDialog.java +++ b/app/src/main/java/com/vondear/tools/activity/ActivityDialog.java @@ -67,19 +67,21 @@ private void initView() { private void initWheelYearMonthDayDialog() { // ------------------------------------------------------------------选择日期开始 - mDialogWheelYearMonthDay = new DialogWheelYearMonthDay(this); + mDialogWheelYearMonthDay = new DialogWheelYearMonthDay(this, 2000,200); mDialogWheelYearMonthDay.getTv_sure().setOnClickListener( new View.OnClickListener() { @Override public void onClick(View arg0) { if (mDialogWheelYearMonthDay.getCheckBox_day().isChecked()) { - mButtonDialogWheelYearMonthDay.setText((mDialogWheelYearMonthDay.getCurYear() - 5) + mDialogWheelYearMonthDay.getYear().getCurrentItem() + "年" - + mDialogWheelYearMonthDay.getMonths()[mDialogWheelYearMonthDay.getMonth().getCurrentItem()] + "月" - + (mDialogWheelYearMonthDay.getDay().getCurrentItem() + 1) + "日"); + mButtonDialogWheelYearMonthDay.setText( + mDialogWheelYearMonthDay.getSelectorYear() + "年" + + mDialogWheelYearMonthDay.getSelectorMonth() + "月" + + mDialogWheelYearMonthDay.getSelectorDay() + "日"); } else { - mButtonDialogWheelYearMonthDay.setText((mDialogWheelYearMonthDay.getCurYear() - 5) + mDialogWheelYearMonthDay.getYear().getCurrentItem() + "年" - + mDialogWheelYearMonthDay.getMonths()[mDialogWheelYearMonthDay.getMonth().getCurrentItem()] + "月"); + mButtonDialogWheelYearMonthDay.setText( + mDialogWheelYearMonthDay.getSelectorYear() + "年" + + mDialogWheelYearMonthDay.getSelectorMonth() + "月"); } mDialogWheelYearMonthDay.cancel(); } @@ -142,7 +144,9 @@ public void onClick(View v) { dialogEditTextSureCancle.show(); break; case R.id.button_DialogWheelYearMonthDay: - initWheelYearMonthDayDialog(); + if (mDialogWheelYearMonthDay == null) { + initWheelYearMonthDayDialog(); + } mDialogWheelYearMonthDay.show(); break; case R.id.button_DialogShapeLoading: