View之DateTimePicker

DateTimePicker

2016/1/29 16:02:08

1. 布局

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            <TextView android:id="@+id/dateDisplay"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="时间显示"/>
        </LinearLayout>

        <Button android:id="@+id/pickDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="改变日期"/>

        <Button android:id="@+id/pickTime12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="改变时间(12小时制)"/>

        <Button android:id="@+id/pickTime24"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="改变时间24小时制"/>

    </LinearLayout>

2. 配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.lgq.datewidget"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="21"
            android:targetSdkVersion="22" />

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Material" >  //Material Design设计风格
            <activity
                android:name=".DateWidgets1"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

3. 实现

    public class DateWidgets1 extends Activity {

        // where we display the selected date and time
        private TextView mDateDisplay;

        // date and time
        private int mYear;
        private int mMonth;
        private int mDay;
        private int mHour;
        private int mMinute;

        static final int TIME_12_DIALOG_ID = 0;
        static final int TIME_24_DIALOG_ID = 1;
        static final int DATE_DIALOG_ID = 2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.date_widgets_example_1);

            mDateDisplay = (TextView) findViewById(R.id.dateDisplay);

            setDialogOnClickListener(R.id.pickDate, DATE_DIALOG_ID);
            setDialogOnClickListener(R.id.pickTime12, TIME_12_DIALOG_ID);
            setDialogOnClickListener(R.id.pickTime24, TIME_24_DIALOG_ID);

            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            updateDisplay();
        }

        private void setDialogOnClickListener(int buttonId, final int dialogId) {
            Button b = (Button) findViewById(buttonId);
            b.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    showDialog(dialogId);
                }
            });
        }

        /**
         * 创建Dialog
         */
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
                case TIME_12_DIALOG_ID:
                case TIME_24_DIALOG_ID:
                    return new TimePickerDialog(this,
                            mTimeSetListener, mHour, mMinute, id == TIME_24_DIALOG_ID);
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this,
                                mDateSetListener,
                                mYear, mMonth, mDay);
            }
            return null;
        }

        /**
         * 显示Dialog
         */

        @Override
        protected void onPrepareDialog(int id, Dialog dialog) {
            switch (id) {
                case TIME_12_DIALOG_ID:
                case TIME_24_DIALOG_ID:
                    ((TimePickerDialog) dialog).updateTime(mHour, mMinute);
                    break;
                case DATE_DIALOG_ID:
                    ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
                    break;
            }
        }    

        private void updateDisplay() {
            mDateDisplay.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mMonth + 1).append("-")
                        .append(mDay).append("-")
                        .append(mYear).append(" ")
                        .append(pad(mHour)).append(":")
                        .append(pad(mMinute)));
        }


        /**
         * 日期设置监听
         */
        private DatePickerDialog.OnDateSetListener mDateSetListener =
                new DatePickerDialog.OnDateSetListener() {

                    public void onDateSet(DatePicker view, int year, int monthOfYear,
                            int dayOfMonth) {
                        mYear = year;
                        mMonth = monthOfYear;
                        mDay = dayOfMonth;
                        updateDisplay();
                    }
                };

        /**
         * 时间设置监听
         */
        private TimePickerDialog.OnTimeSetListener mTimeSetListener =
                new TimePickerDialog.OnTimeSetListener() {

                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        mHour = hourOfDay;
                        mMinute = minute;
                        updateDisplay();
                    }
                };

        private static String pad(int c) {
            if (c >= 10)
                return String.valueOf(c);
            else
                return "0" + String.valueOf(c);
        }
    }

4. 效果

这里写图片描述

日期选择

这里写图片描述

时间选择

  • 12小时制

这里写图片描述

  • 24小时制

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值