自定义datePicker的实现

来源:互联网 发布:卖衣服游戏无网络 编辑:程序博客网 时间:2024/04/29 12:30

在工作中发现Android原生的datePicker和timePicker存在一些问题,并不好用,所以自己编写了自定义的datePickerActivity来满足需求。希望能给其他需要的人提供借鉴,让自己以后遇到类似问题可以很方便的直接使用。其效果如图:

这里写图片描述

细节讲解

  1. 移动焦点时对组件背景的改变
yearPicker.setOnFocusChangeListener(new View.OnFocusChangeListener(){            @Override            public void onFocusChange(View view, boolean b) {                if (getCurrentFocus() == view){                    Log.i(TAG,"---yearPicker---");                 yearPicker.setBackground(getResources().getDrawable(R.drawable.button1));                }                else{                    Log.i(TAG,"---loseYearPicker---");                    yearPicker.setBackgroundColor(Color.alpha(R.color.colorNone));                }            }        });

代码
yearPicker.setBackground(getResources().getDrawable(R.drawable.button1));
在不适合API 16以下的版本,所以可以用
yearPicker.setBackgroundDrawable(getResources().getDrawable(R.drawable.button1));
代替。

使用方法

  1. 点击“确定”按钮返回日期和时间(String类型)。通过intent传递bundle。
  2. 点击“取消”按钮不返回数据。
  3. 在styles.xml中添加MyDialogStyle。
  4. 在drawable中添加图片。
  5. 在AndroidManifest.xml中添加代码:
    <activity android:name=".DatePickerActivity"
    android:theme="@style/MyDialogStyle" />
  6. 使用如下方法来启动datePickerActivity:
 startActivityForResult(new Intent(XXXActivity.this,DatePickerActivity.class),0);

代码

datePickerActivity代码

图片资源

按钮图片1
按钮图片2
按钮图片3
按钮图片4
按钮图片5
按钮图片6

1 0
原创粉丝点击