安卓自定义日期时间选择控件

1.   首先自己写一个xml界面用来显示时间选择器

 - 这里使用了DatePicker控件年月日TimePicker分钟秒控件,因为业务需求我需要精确到秒的选择

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <DatePicker
            android:id="@+id/choose_date"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:paddingLeft="10dp"
            android:calendarViewShown="false">DatePicker>

        <TimePicker
            android:id="@+id/choose_time"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:calendarViewShown="false">TimePicker>
    LinearLayout>
2.  在线程中自己写一个点击事件然后触发弹出页面的事件
    -  对View不熟悉的可以看这篇文章http://blog.csdn.net/guolin_blog/article/details/12921889
//获取一个布局
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_data, null);
        final DatePicker date = (DatePicker) view.findViewById(R.id.choose_date);
        final TimePicker time = (TimePicker) view.findViewById(R.id.choose_time);
        date.init(mYear,mMonth,mDay,null);
        time.setIs24HourView(true);
        //获取一个弹出框
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置标题
        builder.setTitle("选择时间");
        //设置弹出哪个界面
        builder.setView(view);
        //设置尾部的显示,第一个为显示内容,第二个是要绑定的监听事件
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int month = date.getMonth();
                mYear=date.getYear();
                mMonth=month;
                mDay=date.getDayOfMonth();
                mHour=time.getCurrentHour();
                mMinute=time.getCurrentMinute();
                if(dateInput.equals("begin")){
                    displayDate(1);
                }else{
                    displayDate(2);
                }
            }
        });
        builder.setNegativeButton("取消", null);
        //创建弹出对象
        AlertDialog dialog = builder.create();
        //弹出界面
        dialog.show();
        //自动弹出键盘问题解决
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        DateTimeUtil.resizePikcer(date);//调整datepicker大小
        DateTimeUtil.resizePikcer(time);//调整timepicker大小
    }
3.  写一个时间控件调整大小的工具类
    -  这部分不懂的可以看这篇文章http://blog.csdn.net/lizebin_bin/article/details/53582111
public class DateTimeUtil {
    public static void resizePikcer(FrameLayout tp){
        List npList = findNumberPicker(tp);
        for(NumberPicker np:npList){
            resizeNumberPicker(np);
        }
    }
    /**
     * 得到viewGroup里面的numberpicker组件
     * @param viewGroup
     * @return
     */
    private static List findNumberPicker(ViewGroup viewGroup){
        List npList = new ArrayList();
        View child = null;
        if(null != viewGroup){
            for(int i = 0;iif(child instanceof NumberPicker){
                    npList.add((NumberPicker)child);
                }
                else if(child instanceof LinearLayout){
                    List result = findNumberPicker((ViewGroup)child);
                    if(result.size()>0){
                        return result;
                    }
                }
            }
        }
        return npList;
    }
    /*
 * 调整numberpicker大小
 */
    private static void resizeNumberPicker(NumberPicker np){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(155, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 0, 5, 0);
        np.setLayoutParams(params);
    }
}

你可能感兴趣的:(android)