Android中的DatePicker和TimePicker控件使用

编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/52791649

Demo展示图片

Android中的DatePicker和TimePicker控件使用_第1张图片

布局代码

//(layout)activity_main
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.picker.MainActivity">
    <RelativeLayout
        android:id="@+id/actionBar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/colorPrimary">
        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:textSize="16sp"
            android:textColor="#fff"
            android:layout_marginLeft="10dp"
            android:layout_alignParentLeft="true"/>

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:textSize="16sp"
            android:textColor="#fff"
            android:layout_marginRight="10dp"
            android:layout_alignParentRight="true"/>

        <LinearLayout
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:text="设置日期"
                android:textSize="14sp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:onClick="setDate"/>

            <Button
                android:text="设置时间"
                android:textSize="14sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="setTime"/>

        LinearLayout>
    RelativeLayout>
    <TextView
        android:id="@+id/check"
        android:textSize="16sp"
        android:textColor="#000"
        android:layout_marginLeft="10dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_below="@+id/actionBar"
        android:layout_width="match_parent"
        android:layout_height="20dp"/>

    <DatePicker
        android:id="@+id/datePicker"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/check"
        android:layout_alignParentStart="true"/>

    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/datePicker"/>
RelativeLayout>
-------------------------------------------------------------------
//(values)styles
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary
        "colorPrimaryDark">@color/colorPrimaryDark
        "colorAccent">@color/colorAccent
    style>
resources>
//注:将ActionBar隐藏,自定义了一个ActionBar

Activity代码

//MainActivity
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.util.Calendar;
import static com.test.picker.R.id.datePicker;
public class MainActivity extends AppCompatActivity {
    private TimePicker mTimePicker;
    private DatePicker mDatePicker;
    private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMinute;
    private int mSecond;
    private Calendar mCalendar;
    private TextView mData;
    private TextView mTime;
    private TextView mCheck;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initView(){
        mDatePicker = (DatePicker) findViewById(datePicker);
        mTimePicker = (TimePicker) findViewById(R.id.timePicker);
        mData = (TextView) findViewById(R.id.date);
        mTime = (TextView) findViewById(R.id.time);
        mCheck = (TextView) findViewById(R.id.check);
        // 从日历类中获取默认时间(即当前时间)
        mCalendar = Calendar.getInstance();
        mYear = mCalendar.get(Calendar.YEAR);
        // 按照外国习惯,月份是从0 开始的
        mMonth = mCalendar.get(Calendar.MONTH) + 1;
        mDay = mCalendar.get(Calendar.DAY_OF_MONTH);
        mHour = mCalendar.get(Calendar.HOUR_OF_DAY);
        mMinute = mCalendar.get(Calendar.MINUTE);
        mSecond = mCalendar.get(Calendar.SECOND);
    }

    private void initData() {
        // 设置比较时间 没有设置监听器 时间不会随着我们的选择点击而改变
        mCheck.setText(mYear + "-" + addZero(mMonth + "") + "-" + addZero(mDay + "") + "   "
                + addZero(mHour + "") + ":" + addZero(mMinute + "") + ":" + addZero(mSecond + ""));
        // 设置默认时间
        mData.setText(mYear + "-" + addZero(mMonth + "") + "-" + addZero(mDay + ""));
        mTime.setText(mHour + ":" + addZero(mMinute + ""));

        // DatePicker默认把月份加了1,所以月份就不用加1了
        mDatePicker.init(mYear, mMonth - 1, mDay, new DatePicker.OnDateChangedListener() {
            @Override
            // 当日期改变的时候调用的监听
            public void onDateChanged(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                // 监听这里的月份又没有加1,需要手动加1
                int month = (monthOfYear + 1);
                mData.setText(year + "-" + addZero(month + "") + "-" + addZero(dayOfMonth + ""));
            }
        });

        mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hourOfDay, int minute) {
                mTime.setText(addZero(hourOfDay + "") + ":" + addZero(minute + ""));
            }
        });
    }

    // 点击设置日期按钮 弹出日期设置对话框
    public void setDate(View view) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                int month = (monthOfYear + 1);
                mData.setText(year + "-" + addZero(month + "") + "-" + addZero(dayOfMonth + ""));
            }
            //赋默认值(即当前值)
        }, mYear, mMonth - 1, mDay);
        datePickerDialog.show();
    }

    // 点击设置时间按钮 弹出时间设置对话框
    public void setTime(View view){
        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker,  int hourOfDay, int minute) {
                mTime.setText(addZero(hourOfDay + "") + ":" + addZero(minute + ""));
            }
            //赋默认值(即当前值)
        } , mHour , mMinute ,true);
        timePickerDialog.show();
    }

    //时间和日期如果是一位数在前面补一个零
    public String addZero(String num){
        int length = num.length();
        if(length == 1){
            return "0" + num;
        }
        return num;
    }
}

Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9820834


———-因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!———-

你可能感兴趣的:(【Android基础控件使用】,Android开发,TimePicker,DatePicker)