天天看點

隻顯示月和年的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