`
renyuan_1991
  • 浏览: 69429 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

只显示年月的DatePicker

阅读更多
转载请注明出处:http://renyuan-1991.iteye.com/blogs/2304484
先看一下效果图:

1.不显示天数的DatePicker
2.设置DatePicker的显示日期范围
3.DatePicker的样式调整
下面看具体代码:
这种效果的实现首先要隐藏最后一列的Number(DatePicker的三列都是用number实现的)。隐藏最后一列有两种方法,如下:
    第一种:
   
DatePicker dp = findDatePicker((ViewGroup) datePickerDialog.getWindow().getDecorView());
        if (dp != null) {
            ((ViewGroup)((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
        }
    }
private DatePicker findDatePicker(ViewGroup group) {
    if (group != null) {
        for (int i = 0, j = group.getChildCount(); i < j; i++) {
            View child = group.getChildAt(i);
            if (child instanceof DatePicker) {
                return (DatePicker) child;
            } else if (child instanceof ViewGroup) {
                DatePicker result = findDatePicker((ViewGroup) child);
                if (result != null)
                    return result;
            }
        }
    }
    return null;
}
    

这种方法是在DatePickerDialog中使用的,通过findDatePicker方法拿到DatePicker,然后根据DatePicker的布局层次拿到显示天数的那个number并将其设置成GONE。如果我们是自己再布局中添加的DatePicker可以直接通过DatePicker的getChildAt方法拿到显示天数的number并设置成不显示。本例中通过如下方法设置:
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="180dp"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="5dip"
    android:paddingTop="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_marginBottom="-15dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="选择月份" />

    <DatePicker
        android:theme="@android:style/Theme.Holo.Light"
        android:id="@+id/datePickerStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:calendarViewShown="false" />
    <Button
        android:id="@+id/dd_btn_sure"
        android:layout_width="match_parent"
        android:layout_marginTop="-20dp"
        android:text="确 定"
        android:textSize="18sp"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content" />
</LinearLayout>

如果布局文件时以上这样的,我们可以再逻辑代码中直接findviewbyid拿到DatePicker,然后调用以下方法隐藏天数的控件。
.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);

完整的逻辑代码:
public class MyChooseMonthDialog extends AlertDialog implements OnDateChangedListener, View.OnClickListener {
    private static final String YEAR = "YEAR";
    private static final String MONTH = "MONTH";
    private static final String DAY = "DAY";

    private final DatePicker mDatePicker;
    private final MyChooseMonthDialog.OnDateSetListener mCallBack;


    /**
     * The callback used to indicate the user is done filling in the date.
     */
    public interface OnDateSetListener {
        void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth);
    }

    /**
     * @param context     The context the dialog is to run in.
     * @param callBack    How the parent is notified that the date is set.
     * @param year        The initial year of the dialog.
     * @param monthOfYear The initial month of the dialog.
     * @param dayOfMonth  The initial day of the dialog.
     */
    public MyChooseMonthDialog(Context context, MyChooseMonthDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        this(context, 0, callBack, year, monthOfYear, dayOfMonth);
    }

    public MyChooseMonthDialog(Context context, int theme, MyChooseMonthDialog.OnDateSetListener callBack, int year, int monthOfYear,
                               int dayOfMonth) {
        this(context, 0, callBack, year, monthOfYear, dayOfMonth, true);
    }

    /**
     * @param context     The context the dialog is to run in.
     * @param theme       the theme to apply to this dialog
     * @param callBack    How the parent is notified that the date is set.
     * @param year        The initial year of the dialog.
     * @param monthOfYear The initial month of the dialog.
     * @param dayOfMonth  The initial day of the dialog.
     */
    public MyChooseMonthDialog(Context context, int theme, MyChooseMonthDialog.OnDateSetListener callBack, int year, int monthOfYear,
                               int dayOfMonth, boolean isDayVisible) {
        super(context, theme);
        mCallBack = callBack;
        Context themeContext = getContext();
        setIcon(0);
        LayoutInflater inflater = (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.datepicker_dialog, null);
        Button dd_btn_sure = (Button) view.findViewById(R.id.dd_btn_sure);
        dd_btn_sure.setOnClickListener(this);
        setView(view);
        mDatePicker = (DatePicker) view.findViewById(R.id.datePickerStart);
        mDatePicker.init(year, monthOfYear, dayOfMonth, this);
        mDatePicker.setMaxDate(System.currentTimeMillis());
        setMyStyle(mDatePicker);
        // 如果要隐藏当前日期,则使用下面方法。
        if (!isDayVisible) {
            hidDay(mDatePicker);
        }
    }

    private void setMyStyle(DatePicker mDatePicker) {
        LinearLayout llFirst = (LinearLayout) mDatePicker.getChildAt(0);
        LinearLayout llSecond = (LinearLayout) llFirst.getChildAt(0);
        llSecond.setPadding(0, 0,0, 0);
        llSecond.setBackgroundResource(R.color.transparent);//设置datepickerdialog的背景

        for (int i = 0; i < llSecond.getChildCount(); i++) {
            NumberPicker picker = (NumberPicker) llSecond.getChildAt(i); // Numberpickers in llSecond
            Field[] pickerFields = NumberPicker.class.getDeclaredFields();
            for (Field pf : pickerFields) {
                //更改分割线的颜色
                if (pf.getName().equals("mSelectionDivider")) {
                    pf.setAccessible(true);
                    try {
                        pf.set(picker, ContextCompat.getDrawable(YztApplication.getInstance(), R.color.investor_ilp_red));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

    private void hidDay(DatePicker mDatePicker) {
        if(mDatePicker!=null){
            ((ViewGroup)((ViewGroup) mDatePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dd_btn_sure:
                tryNotifyDateSet();
                this.dismiss();
                break;
        }
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        if (view.getId() == R.id.datePickerStart)
            mDatePicker.init(year, month, day, this);
        // updateTitle(year, month, day);
    }

    /**
     * 获得开始日期的DatePicker
     *
     * @return The calendar view.
     */
    public DatePicker getDatePickerStart() {
        return mDatePicker;
    }

    /**
     * Sets the start date.
     *
     * @param year        The date year.
     * @param monthOfYear The date month.
     * @param dayOfMonth  The date day of month.
     */
    public void updateStartDate(int year, int monthOfYear, int dayOfMonth) {
        mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
    }


    private void tryNotifyDateSet() {
        if (mCallBack != null) {
            mDatePicker.clearFocus();
            mCallBack.onDateSet(mDatePicker, mDatePicker.getYear(), mDatePicker.getMonth(),
                    mDatePicker.getDayOfMonth());
        }
    }

    @Override
    protected void onStop() {
        // tryNotifyDateSet();
        super.onStop();
    }

    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putInt(YEAR, mDatePicker.getYear());
        state.putInt(MONTH, mDatePicker.getMonth());
        state.putInt(DAY, mDatePicker.getDayOfMonth());
        return state;
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        int year = savedInstanceState.getInt(YEAR);
        int month = savedInstanceState.getInt(MONTH);
        int day = savedInstanceState.getInt(DAY);
        mDatePicker.init(year,month,day, this);
    }
}

其中比较重要的是以下这一段代码:
private void setMyStyle(DatePicker mDatePicker) {
        LinearLayout llFirst = (LinearLayout) mDatePicker.getChildAt(0);
        LinearLayout llSecond = (LinearLayout) llFirst.getChildAt(0);
        llSecond.setPadding(0, 0,0, 0);
        llSecond.setBackgroundResource(R.color.transparent);//设置datepickerdialog的背景

        for (int i = 0; i < llSecond.getChildCount(); i++) {
            NumberPicker picker = (NumberPicker) llSecond.getChildAt(i); // Numberpickers in llSecond
            Field[] pickerFields = NumberPicker.class.getDeclaredFields();
            for (Field pf : pickerFields) {
                //更改分割线的颜色
                if (pf.getName().equals("mSelectionDivider")) {
                    pf.setAccessible(true);
                    try {
                        pf.set(picker, ContextCompat.getDrawable(YztApplication.getInstance(), R.color.investor_ilp_red));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

这段代码通过DatePicker拿到他的子布局,然后再通过反射拿到NumberPicker中得分割线,并修改其颜色。同理我们也可以通过反射拿到显示天数的那一列并设置成隐藏,这就是第二种方法,代码如下:
private void hidDay(DatePicker mDatePicker) {
         Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
         for (Field datePickerField : datePickerfFields) {
             if ("mDaySpinner".equals(datePickerField.getName())) {
                 datePickerField.setAccessible(true);
                 Object dayPicker = new Object();
                 try {
                     dayPicker = datePickerField.get(mDatePicker);
                 } catch (IllegalAccessException e) {
                     e.printStackTrace();
                 } catch (IllegalArgumentException e) {
                     e.printStackTrace();
                 }
                 // datePicker.getCalendarView().setVisibility(View.GONE);
                 ((View) dayPicker).setVisibility(View.GONE);
             }
         }
     }

以上第二种方法的代码来自:http://www.cnblogs.com/jilianggqq/p/4139510.html
在5.0以后的系统测试没有通过,设置成holo的主题也不行,看了一下原因是由于DatePickerDialog源码中的布局层次跟逻辑代码中反射的逻辑不对应,稍作修改应该可以实现相同的效果。
    当隐藏最后一列的时候AlertDialog并没有调整布局的大小,这个时候看起来很不协调,我们需要再Dialog调用show之后调用setLayout方法,注意一定要在show之后调用,代码如下:
 datePickerDialog.show();
        datePickerDialog.getWindow().setLayout(Utils.dip2px(IncomeDetailActivity.this,260),Utils.dip2px(IncomeDetailActivity.this,300));

设置DatePicker的日期范围:
mDatePicker.setMaxDate(System.currentTimeMillis());

设置日期范围后遇到一个小问题,当DatePicker显示的月份是当前月份的时候,可以看到下一个月,理论上设置了日期范围为今天是不会显示下一个月的,当滑动的时候会看到月份显示立马又正常了。也就是说mDatePicker.setMaxDate之后刷新了年没有刷新月份的显示....具体原因待考究。
希望爱好编程的小伙伴能加这个群,互相帮助,共同学习。群号: 141877583
  • 大小: 102.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics