基于WheelView自定义的DatePickerDialog

本人利用WheelView写的一个DatePickerDialog
(还有一个TimePickerDialog,本人忘了在写在哪个项目里了,等找到了也贴上来)

先看图,有个直观的了解

基于WheelView自定义的DatePickerDialog


DatePickerDialog代码:
import java.util.Calendar;

import com.widget.wheel.NumericWheelAdapter;
import com.widget.wheel.OnWheelScrollListener;
import com.widget.wheel.WheelView;
import com.yirui.youbao.app.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/**
 * 
 * @author pythoner
 * 
 */
public class DatePickerDialog extends Dialog
{

    private Button btn_left, btn_right;

    private WheelView year, month, day;

    private String date;//初始化显示的日期,默认为当日

    public DatePickerDialog(Context context, String date)
    {
//        super(context);
        this(context, R.style.Theme_Dialog_NoTitle, date);
        // TODO Auto-generated constructor stub
    }

    public DatePickerDialog(Context context, int theme, String date)
    {
        super(context, theme);
        // TODO Auto-generated constructor stub
        this.date = date;
        init();
    }

    private void init()
    {
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_date_picker);

        initViews();
        initValues();
    }

    private void initViews()
    {
        btn_left = (Button) findViewById(R.id.btn_left);
        btn_left.setOnClickListener(clickListener);
        btn_right = (Button) findViewById(R.id.btn_right);
        btn_right.setOnClickListener(clickListener);

        String icurYear, icurMonth, icurDay;
        Calendar c = Calendar.getInstance();
        int curYear = c.get(Calendar.YEAR);
        if (date == null || date.length() < 10)
        {// 格式不正确
            int curMonth = c.get(Calendar.MONTH);
            int curDay = c.get(Calendar.DAY_OF_MONTH);
            icurYear = String.valueOf(curYear);
            icurMonth = String.valueOf(curMonth + 1);
            icurDay = String.valueOf(curDay);
        }
        else
        {// 年月日
            icurYear = date.substring(0, 4);
            icurMonth = date.substring(5, 7);
            icurDay = date.substring(8, 10);
        }

        year = (WheelView) findViewById(R.id.year);
        year.setAdapter(new NumericWheelAdapter(curYear - 2, curYear));
        // year.setLabel("年");
        year.setCurrentItem(Integer.parseInt(icurYear) - Integer.parseInt(year.getAdapter().getItem(0)));

        month = (WheelView) findViewById(R.id.month);
        month.setAdapter(new NumericWheelAdapter(1, 12));// "%02d"
        // month.setLabel("月");
        month.setCyclic(true);
        month.setCurrentItem(Integer.parseInt(icurMonth) - 1);

        int daysOfMounth = getDaysOfMounth();
        day = (WheelView) findViewById(R.id.day);
        day.setAdapter(new NumericWheelAdapter(1, daysOfMounth));
        // day.setLabel("日");
        day.setCyclic(true);
        day.setCurrentItem(Integer.parseInt(icurDay) - 1);

        OnWheelScrollListener scrollListener = new OnWheelScrollListener()
        {
            public void onScrollingStarted(WheelView wheel)
            {
            }

            public void onScrollingFinished(WheelView wheel)
            {
                int daysOfMounth = getDaysOfMounth();
                day.setAdapter(new NumericWheelAdapter(1, daysOfMounth));
            }
        };

        year.addScrollingListener(scrollListener);
        month.addScrollingListener(scrollListener);
        // day.addScrollingListener(scrollListener);

    }

    private int getDaysOfMounth()
    {
        int mMonth = Integer.parseInt(month.getAdapter().getItem(month.getCurrentItem()));
        switch (mMonth)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                int mYear = Integer.parseInt(year.getAdapter().getItem(year.getCurrentItem()));
                if (mYear % 4 == 0 && mYear % 100 != 0 || mYear % 400 == 0)
                {
                    return 29;
                }
                else
                {
                    return 28;
                }
        }
        return -1;
    }

    private void initValues()
    {

    }

    View.OnClickListener clickListener = new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            switch (v.getId())
            {
                case R.id.btn_left:
                    dismiss();
                    break;
                case R.id.btn_right:
                    String mYear = year.getAdapter().getItem(year.getCurrentItem());
                    String mMonth = month.getAdapter().getItem(month.getCurrentItem());
                    String mDay = day.getAdapter().getItem(day.getCurrentItem());
                    if (mDay == null || mDay.length() == 0)
                    {
                        mDay = "1";
                    }
                    if (Integer.parseInt(mMonth) < 10)
                    {
                        mMonth = "0" + mMonth;
                    }
                    if (Integer.parseInt(mDay) < 10)
                    {
                        mDay = "0" + mDay;
                    }
                    if (onOKClickListener != null)
                    {
                        onOKClickListener.onOKClick(mYear, mMonth, mDay);
                    }
                    dismiss();
                    break;

                default:
                    break;
            }
        }

    };

    private OnOKClickListener onOKClickListener;

    public interface OnOKClickListener
    {
        public void onOKClick(String year, String month, String date);
    }

    public void setOnOKClickListener(OnOKClickListener onOKClickListener)
    {
        this.onOKClickListener = onOKClickListener;
    }
}


DatePickerDialog的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:gravity="center"
    android:background="@drawable/bg_picker_dialog"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="8dp" 
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:layout_weight="1" 
            android:gravity="center_horizontal"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />

            <com.widget.wheel.WheelView
                android:id="@+id/year"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="10dip"
                android:layout_marginTop="10dip"
                android:layout_weight="1" 
                />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:layout_weight="1" 
            android:gravity="center_horizontal"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />

            <com.widget.wheel.WheelView
                android:id="@+id/month"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="8dip"
                android:layout_marginTop="8dip"
                android:layout_weight="1" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />
        </LinearLayout>
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:layout_weight="1" 
            android:gravity="center_horizontal"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />

            <com.widget.wheel.WheelView
                android:id="@+id/day"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginBottom="8dip"
                android:layout_marginTop="8dip"
                android:layout_weight="1" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:scaleType="fitCenter"
                />
        </LinearLayout>
        
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal"
        android:background="@drawable/line_top_with_blue"
        >

        <Button
            android:id="@+id/btn_left"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="8dip"
            android:paddingTop="8dip"
            android:text="@string/cancel"
            android:textColor="@android:color/black"
            android:textSize="@dimen/font_big" 
            android:background="@drawable/bg_btn_trans_blue_with_no_corner"
            />


        <View 
            android:layout_width="@dimen/line_width"
            android:layout_height="match_parent"
            android:background="@color/primary"
            />
        
        <Button
            android:id="@+id/btn_right"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingBottom="8dip"
            android:paddingTop="8dip"
            android:text="@string/confirm"
            android:textColor="@android:color/black"
            android:textSize="@dimen/font_big" 
            android:background="@drawable/bg_btn_trans_blue_with_no_corner"
            />
    </LinearLayout>

</LinearLayout>


style:
<style name="Theme_Dialog_NoTitle" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>



使用:
dialog = new DatePickerDialog(context, R.style.Theme_Dialog, btn_startDate.getText().toString().trim());
			dialog.setOnOKClickListener(new DatePickerDialog.OnOKClickListener()
            {

                @Override
                public void onOKClick(String year, String month, String date)
                {
                    // TODO Auto-generated method stub
                    //dosomething
                    //btn_startDate.setText(year + "-" + month + "-" + date);
                   
                }
            });
            dialog.show();


WheelView类见附件,直接加入到你的工程中即可

你可能感兴趣的:(android)