天天看点

只显示月和年的DatePickerDialog

   需求要只显示月和年的日历控件,又不想自定义控件,最简单的办法就是隐藏显示日的这个框了,但DatePickerDialog并没有直接提供方法来操作,网上搜了下,并没有找到合适的解决方案。

   农民伯伯的这篇文章:http://www.cnblogs.com/over140/archive/2011/09/20/2181532.html,虽然可以实现,但是当手机不同时此方法就不行。

   首先想自定义一个控件,但是太麻烦,看了下2.2的DatePicker中用到了NumberPicker,但是这个NumberPicker在3.0以下是个隐藏控件没法用。因而重写DatePicker来实现自定义太过麻烦了。

   有没更好的方法呢?看了下农民伯伯那篇博客发现只要隐藏“年”这个NumberPicker就可以了,可是用

((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
           

 一旦换个手机,或者切换语言时,就会发现年所在的位置会发生变化,因此次方法并不可取,那么如果我能够直接获取“年”的NumberPicker,然后隐藏不就可以了,搜了下,发现利用java的反射可以达到此目的

Class c=dp.getClass();
	        	Field f;
				try {
					f = c.getDeclaredField("mDayPicker" );
					f.setAccessible(true );  
					LinearLayout l= (LinearLayout)f.get(dp);   
					l.setVisibility(View.GONE);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
           

这样就避免了手机适配的问题了。

自定义的DatePickerDialog完整代码

/**
 * 
 */
package com.small.shopkeeper.view;

import java.lang.reflect.Field;

import android.app.DatePickerDialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.LinearLayout;
/**
 * @author liujun
 * 自定义的日期控件,只有年和月,没有日
 * 2012-10-17 上午11:02:17 
 */
public class YMPickerDialog extends DatePickerDialog {
	 public YMPickerDialog(Context context,
             OnDateSetListener callBack, int year, int monthOfYear) {
         super(context, callBack, year, monthOfYear, 3);
         this.setTitle(year+"年"+(monthOfYear + 1) + "月" );
     }

     @Override
     public void onDateChanged(DatePicker view, int year, int month, int day) {
         super.onDateChanged(view, year, month, day);
         this.setTitle(year+"年"+(month + 1) + "月" );
     }

	/* (non-Javadoc)
	 * @see android.app.DatePickerDialog#show()
	 */
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		 DatePicker dp = findDatePicker((ViewGroup) this.getWindow().getDecorView());
	        if (dp != null) {
	        	Class c=dp.getClass();
	        	Field f;
				try {
					f = c.getDeclaredField("mDayPicker" );
					f.setAccessible(true );  
					LinearLayout l= (LinearLayout)f.get(dp);   
					l.setVisibility(View.GONE);
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchFieldException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
	        	
	        } 
	}
	/**
     * 从当前Dialog中查找DatePicker子控件
     * 
     * @param group
     * @return
     */
    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;

    } 
      
}
           

本文参考:http://www.cnblogs.com/over140/archive/2011/09/20/2181532.html

               http://www.189works.com/article-31463-1.html