Android DatePickerDialog 只显示年月

今天写一个日期控件,默认显示年月日,但是我现在只想显示年月,在网上找了一个比较简单容易了理解的方法,分享如下:

先看一个效果图:
处理前:   处理后:
实现的代码:

    1. 通过遍历方法查找DatePicker里的子控件:年、月、日
 
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;
    } 

2.隐藏不想显示的子控件,这里隐藏日控件
        final Calendar cal = Calendar.getInstance();
        mDialog = new CustomerDatePickerDialog(getContext(), this,
            cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
            cal.get(Calendar.DAY_OF_MONTH));
        mDialog.show();
        
        DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
        if (dp != null) {
            ((ViewGroup)((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
        } 

如果想隐藏年,把 getChildAt(2)改为getChildAt(0)...

  3.补充:如果标题栏也想改,需要自定义并实现onDateChanged方法才可实现,代码:
    class CustomerDatePickerDialog extends DatePickerDialog {
        public CustomerDatePickerDialog(Context context,
                OnDateSetListener callBack, int year, int monthOfYear,
                int dayOfMonth) {
            super(context, callBack, year, monthOfYear, dayOfMonth);
        }

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

 
public class Android_DateActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ private Button button; private TextView textview; private Dialog mdialog; private Calendar calendar = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); initUi(); button.setOnClickListener(this); } public void initUi() { button = (Button) findViewById(R.id.button); textview = (TextView) findViewById(R.id.textview); } @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(0);// 日期弹出框 int SDKVersion = Android_DateActivity.this.getSDKVersionNumber();// 获取系统版本 System.out.println("SDKVersion = " + SDKVersion); DatePicker dp = findDatePicker((ViewGroup) mdialog.getWindow() .getDecorView());// 设置弹出日 if (dp != null) { if (SDKVersion 14) { ((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)) .getChildAt(1).setVisibility(View.GONE); } } } @Override protected Dialog onCreateDialog(int id) { // 对应上面的showDialog(0);//日期弹出框 mdialog = null; switch (id) { case 0: calendar = Calendar.getInstance(); mdialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { textview.setText(year + "-" + (monthOfYear + 1)); } }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); break; } return mdialog; } /** * 从当前Dialog中查找DatePicker子控件 * * @param group * @return */ private DatePicker findDatePicker(ViewGroup group) { if (group != null) { for (int i = 0, j = group.getChildCount()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值