设计原则:方法回调
最开始设计:完成了NumberPicker这个工程类的选择,当时忘记了把值传递给调用的Activtiy/Fragment,之后,将案例用于实际工程中,便无法取得当前的值,于是:傻了,然后想到回调,我目前知道的回调的方法就是通过Activtiy回调,但是,这里无法用Activtiy回调……
有幸得靖哥,写了当前的这种回调方式:不能理解深入,方案是:
1、让功能类内部有一个接口,接口里有一个处理数据的方法,设计构造方法的时,把功能类设计到构造方法里面
2、调用类实现功能类内部的接口,并且实现方法,(主要目的还是为了功能类能调用当前方法)
3、当Dialog点击确定的时候,将传递过来的功能类,调用自身方法,这里数据便回调给了Fragment(可能理解错误,)
再分析:回调是:功能类调用Fragment中的方法,处理数据
布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_horizontal">
<NumberPicker
android:id="@+id/np_year"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginRight="10dp"/>
<NumberPicker
android:id="@+id/np_month"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_marginLeft="10dp"/>
</LinearLayout>
代码
package com.sysweal.marketing.bogy.support.utils;
import java.util.Calendar;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.Toast;
import android.widget.NumberPicker.OnValueChangeListener;
import com.sysweal.marketing.bogy.R;
public class DatePickerDialogUtil {
<span style="white-space:pre"> </span>private Context ct;
<span style="white-space:pre"> </span>private String title;
<span style="white-space:pre"> </span>private String yearValue;
<span style="white-space:pre"> </span>private String monthValue;
<span style="white-space:pre"> </span>private int monthTempMaxValue;
<span style="white-space:pre"> </span>private boolean flagDate = true;
<span style="white-space:pre"> </span>private String seasonValue;
<span style="white-space:pre"> </span>private boolean flagSeason = true;
<span style="white-space:pre"> </span>public String yearClick;
<span style="white-space:pre"> </span>public String monthClick;
<span style="white-space:pre"> </span>public String seasonClick;
<span style="white-space:pre"> </span>DatePickerCallback callback;
<span style="white-space:pre"> </span>public DatePickerDialogUtil(Context ct, String title,DatePickerCallback callback) {
<span style="white-space:pre"> </span>super();
<span style="white-space:pre"> </span>this.ct = ct;
<span style="white-space:pre"> </span>this.title = title;
<span style="white-space:pre"> </span>this.callback = callback;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String[] getDate(){
<span style="white-space:pre"> </span>showDateDialog().show();
<span style="white-space:pre"> </span>String[] dateAry = new String[2];
<span style="white-space:pre"> </span>if(yearClick != null){
<span style="white-space:pre"> </span>dateAry[0] = yearClick;
<span style="white-space:pre"> </span>dateAry[1] = monthClick;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return dateAry;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/** 日期选择 **/
<span style="white-space:pre"> </span>public AlertDialog showDateDialog() {
<span style="white-space:pre"> </span>AlertDialog.Builder builder = new AlertDialog.Builder(ct);
<span style="white-space:pre"> </span>builder.setMessage(title);
<span style="white-space:pre"> </span>builder.setCancelable(false);
<span style="white-space:pre"> </span>builder.setView(dateView());
<span style="white-space:pre"> </span>builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
<span style="white-space:pre"> </span>public void onClick(DialogInterface dialog, int id) {
<span style="white-space:pre"> </span>yearClick =( yearValue == null ? yearClick: yearValue);
<span style="white-space:pre"> </span>monthClick = ( monthValue == null ? monthClick: monthValue);
<span style="white-space:pre"> </span>//不点击,其实已经点击
<span style="white-space:pre"> </span>Calendar calendar = Calendar.getInstance();
<span style="white-space:pre"> </span>int yearValueCal = calendar.get(Calendar.YEAR);
<span style="white-space:pre"> </span>int monthValueCal = calendar.get(Calendar.MONTH) + 1;
<span style="white-space:pre"> </span>if(yearClick == null || yearClick.equals("")){
<span style="white-space:pre"> </span>yearClick = yearValueCal + "";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(monthClick == null || monthClick.equals("")){
<span style="white-space:pre"> </span>monthClick = monthValueCal + "";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(callback != null){
<span style="white-space:pre"> </span>if(Integer.valueOf(monthClick) < 10){
<span style="white-space:pre"> </span>monthClick = "0" + monthClick;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>callback.dealYM(yearClick,monthClick);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>//Toast.makeText(ct, yearClick+":"+monthClick, 1).show();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
<span style="white-space:pre"> </span>public void onClick(DialogInterface dialog, int id) {
<span style="white-space:pre"> </span>dialog.cancel();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>AlertDialog alert = builder.create();
<span style="white-space:pre"> </span>return alert;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public interface DatePickerCallback{
<span style="white-space:pre"> </span>public void dealYM(String year,String month);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/** 季度选择 **/
<span style="white-space:pre"> </span>public AlertDialog showSeasonDialog() {
<span style="white-space:pre"> </span>AlertDialog.Builder builder = new AlertDialog.Builder(ct);
<span style="white-space:pre"> </span>builder.setMessage(title);
<span style="white-space:pre"> </span>builder.setCancelable(false);
<span style="white-space:pre"> </span>builder.setView(seasonView());
<span style="white-space:pre"> </span>builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
<span style="white-space:pre"> </span>public void onClick(DialogInterface dialog, int id) {
<span style="white-space:pre"> </span>yearClick = (yearValue == null ? yearClick : yearValue);
<span style="white-space:pre"> </span>seasonClick = (seasonValue == null ? seasonClick : seasonValue);
<span style="white-space:pre"> </span>//不点击滚动,其实默认选择了
<span style="white-space:pre"> </span>if(yearClick == null || yearClick.equals("")){
<span style="white-space:pre"> </span>Calendar calendar = Calendar.getInstance();
<span style="white-space:pre"> </span>int yearValueCal = calendar.get(Calendar.YEAR);
<span style="white-space:pre"> </span>yearClick = yearValueCal + "";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(seasonClick == null || seasonClick.equals("")){
<span style="white-space:pre"> </span>Calendar calendar = Calendar.getInstance();
<span style="white-space:pre"> </span>int monthValueCal = calendar.get(Calendar.MONTH) + 1;
<span style="white-space:pre"> </span>int seasonDefaultValue = 0;
<span style="white-space:pre"> </span>if(monthValueCal <= 3){
<span style="white-space:pre"> </span>seasonDefaultValue = 0;
<span style="white-space:pre"> </span>}else if(3<monthValueCal && monthValueCal <= 6){
<span style="white-space:pre"> </span>seasonDefaultValue = 1;
<span style="white-space:pre"> </span>}else if(6<monthValueCal && monthValueCal <= 9){
<span style="white-space:pre"> </span>seasonDefaultValue = 2;
<span style="white-space:pre"> </span>}else if(9<monthValueCal){
<span style="white-space:pre"> </span>seasonDefaultValue = 3;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>seasonClick = seasonDefaultValue + "";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(callback != null){
<span style="white-space:pre"> </span>callback.dealYM(yearClick,seasonClick);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
<span style="white-space:pre"> </span>public void onClick(DialogInterface dialog, int id) {
<span style="white-space:pre"> </span>dialog.cancel();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>AlertDialog alert = builder.create();
<span style="white-space:pre"> </span>return alert;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 创造一个日期选择的view
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public View dateView() {
<span style="white-space:pre"> </span>Calendar calendar = Calendar.getInstance();
<span style="white-space:pre"> </span>final int yearValueCal = calendar.get(Calendar.YEAR);
<span style="white-space:pre"> </span>final int monthValueCal = calendar.get(Calendar.MONTH) + 1;
<span style="white-space:pre"> </span>View view = LayoutInflater.from(ct).inflate(R.layout.dateselectdialog,null);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>final NumberPicker yearPicker = (NumberPicker) view.findViewById(R.id.np_year);
<span style="white-space:pre"> </span>final NumberPicker monthPicker = (NumberPicker) view.findViewById(R.id.np_month);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>yearPicker.setMinValue(yearValueCal - 20);
<span style="white-space:pre"> </span>yearPicker.setMaxValue(yearValueCal);
<span style="white-space:pre"> </span>yearPicker.setValue(yearValueCal);
<span style="white-space:pre"> </span>yearPicker.setWrapSelectorWheel(false);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>yearPicker.setOnValueChangedListener(new OnValueChangeListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
<span style="white-space:pre"> </span>yearValue = newVal+"";
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 对后面的月做处理
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>if(yearValue != null){
<span style="white-space:pre"> </span>flagDate = false;
<span style="white-space:pre"> </span>monthPicker.setMinValue(1);
<span style="white-space:pre"> </span>if(yearValueCal > Integer.valueOf(yearValue)){
<span style="white-space:pre"> </span>monthTempMaxValue = 12;
<span style="white-space:pre"> </span>}else{
<span style="white-space:pre"> </span>monthTempMaxValue = monthValueCal;
<span style="white-space:pre"> </span>if(monthValue != null){
<span style="white-space:pre"> </span>if(Integer.valueOf(monthValue) > monthValueCal ){
<span style="white-space:pre"> </span>monthValue = monthValueCal+"";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>monthPicker.setMaxValue(monthTempMaxValue);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>//------------------------------------------------------------------------------------
<span style="white-space:pre"> </span>//默认
<span style="white-space:pre"> </span>if(flagDate){
<span style="white-space:pre"> </span>monthPicker.setMinValue(1);
<span style="white-space:pre"> </span>monthPicker.setMaxValue(monthValueCal);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>monthPicker.setValue(monthValueCal);
<span style="white-space:pre"> </span>monthPicker.setWrapSelectorWheel(true);
<span style="white-space:pre"> </span>monthPicker.setOnValueChangedListener(new OnValueChangeListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
<span style="white-space:pre"> </span>monthValue = newVal+"";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return view;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 创造一个季度选择的view
<span style="white-space:pre"> </span> * 季度选择输出值为:0,1,2,3,代表四个季度
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>public View seasonView() {
<span style="white-space:pre"> </span>Calendar calendar = Calendar.getInstance();
<span style="white-space:pre"> </span>final int yearValueCal = calendar.get(Calendar.YEAR);
<span style="white-space:pre"> </span>final int monthValueCal = calendar.get(Calendar.MONTH) + 1;
<span style="white-space:pre"> </span>View view = LayoutInflater.from(ct).inflate(R.layout.dateselectdialog,null);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>final NumberPicker yearPicker = (NumberPicker) view.findViewById(R.id.np_year);
<span style="white-space:pre"> </span>final NumberPicker seasonPicker = (NumberPicker) view.findViewById(R.id.np_month);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>yearPicker.setMinValue(yearValueCal - 20);
<span style="white-space:pre"> </span>yearPicker.setMaxValue(yearValueCal);
<span style="white-space:pre"> </span>yearPicker.setValue(yearValueCal);
<span style="white-space:pre"> </span>yearPicker.setWrapSelectorWheel(false);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>yearPicker.setOnValueChangedListener(new OnValueChangeListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
<span style="white-space:pre"> </span>yearValue = newVal+"";
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>/**
<span style="white-space:pre"> </span> * 对后面的季度做处理
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>if(yearValue != null){
<span style="white-space:pre"> </span>flagSeason = false;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>seasonPicker.setMinValue(0);
<span style="white-space:pre"> </span>if(yearValueCal > Integer.valueOf(yearValue)){
<span style="white-space:pre"> </span>String[] seasonAry = {"一季度","二季度","三季度","四季度"};
<span style="white-space:pre"> </span>seasonPicker.setDisplayedValues(seasonAry);
<span style="white-space:pre"> </span>seasonPicker.setMaxValue(seasonAry.length - 1);
<span style="white-space:pre"> </span>}else{
<span style="white-space:pre"> </span>int seasonDefaultValue = 0;
<span style="white-space:pre"> </span>if(monthValueCal <= 3){
<span style="white-space:pre"> </span>seasonDefaultValue = 0;
<span style="white-space:pre"> </span>}else if(3<monthValueCal && monthValueCal <= 6){
<span style="white-space:pre"> </span>seasonDefaultValue = 1;
<span style="white-space:pre"> </span>}else if(6<monthValueCal && monthValueCal <= 9){
<span style="white-space:pre"> </span>seasonDefaultValue = 2;
<span style="white-space:pre"> </span>}else if(9<monthValueCal){
<span style="white-space:pre"> </span>seasonDefaultValue = 3;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>String[] seasonAry2 = new String[seasonDefaultValue+1];
<span style="white-space:pre"> </span>for (int i = 0; i < seasonAry2.length; i++) {
<span style="white-space:pre"> </span>String temp = "";
<span style="white-space:pre"> </span>switch (i) {
<span style="white-space:pre"> </span>case 0 : temp = "一" ;break;
<span style="white-space:pre"> </span>case 1 : temp = "二" ;break;
<span style="white-space:pre"> </span>case 2 : temp = "三" ;break;
<span style="white-space:pre"> </span>case 3 : temp = "四" ;break;
<span style="white-space:pre"> </span>default : temp = "一" ;break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>seasonAry2[i] = temp+"季度";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(seasonValue != null){
<span style="white-space:pre"> </span>if(Integer.valueOf(seasonValue) > seasonDefaultValue){
<span style="white-space:pre"> </span>seasonValue = seasonDefaultValue +"";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>seasonPicker.setMaxValue(seasonAry2.length-1);
<span style="white-space:pre"> </span>seasonPicker.setDisplayedValues(seasonAry2);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>//------------------------------------------------------------------------------------
<span style="white-space:pre"> </span>//默认
<span style="white-space:pre"> </span>int seasonDefaultValue = 0;
<span style="white-space:pre"> </span>if(monthValueCal <= 3){
<span style="white-space:pre"> </span>seasonDefaultValue = 0;
<span style="white-space:pre"> </span>}else if(3<monthValueCal && monthValueCal <= 6){
<span style="white-space:pre"> </span>seasonDefaultValue = 1;
<span style="white-space:pre"> </span>}else if(6<monthValueCal && monthValueCal <= 9){
<span style="white-space:pre"> </span>seasonDefaultValue = 2;
<span style="white-space:pre"> </span>}else if(9<monthValueCal){
<span style="white-space:pre"> </span>seasonDefaultValue = 3;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(flagSeason){
<span style="white-space:pre"> </span>seasonPicker.setMinValue(0);
<span style="white-space:pre"> </span>String[] seasonAry = new String[seasonDefaultValue+1];
<span style="white-space:pre"> </span>for (int i = 0; i < seasonAry.length; i++) {
<span style="white-space:pre"> </span>String temp = "";
<span style="white-space:pre"> </span>switch (i) {
<span style="white-space:pre"> </span>case 0 : temp = "一" ;break;
<span style="white-space:pre"> </span>case 1 : temp = "二" ;break;
<span style="white-space:pre"> </span>case 2 : temp = "三" ;break;
<span style="white-space:pre"> </span>case 3 : temp = "四" ;break;
<span style="white-space:pre"> </span>default : temp = "一" ;break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>seasonAry[i] = temp+"季度";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>seasonPicker.setMaxValue(seasonAry.length-1);
<span style="white-space:pre"> </span>seasonPicker.setDisplayedValues(seasonAry);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>seasonPicker.setValue(seasonDefaultValue);
<span style="white-space:pre"> </span>seasonPicker.setWrapSelectorWheel(false);
<span style="white-space:pre"> </span>seasonPicker.setOnValueChangedListener(new OnValueChangeListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
<span style="white-space:pre"> </span>seasonValue = newVal+"";
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return view;
<span style="white-space:pre"> </span>}
}