Android DatePicker 使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013420428/article/details/64131415


DatePicker相关属性:

android:startYear="2000"
android:endYear="2018"
android:calendarViewShown="true"
android:spinnersShown="true"
android:maxDate="2020-02-02"
android:minDate="1987-08-16"



package shortcut.song.com.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;


public class PickerDialogActivity extends AppCompatActivity {



    int year;
    int month;
    int day;
    int hour;
    int minute;
    TextView mTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picker_dialog);

        DatePicker mDatePicker;
        TimePicker mTimePicker;
        mDatePicker = (DatePicker)findViewById(R.id.picker_date);
        mTimePicker = (TimePicker)findViewById(R.id.picker_time);
        mTextView = (TextView)findViewById(R.id.tv_picker_show);

        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        hour = c.get(Calendar.HOUR);
        month = c.get(Calendar.MONTH);

        mDatePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                PickerDialogActivity.this.year = year;
                PickerDialogActivity.this.month = monthOfYear;
                PickerDialogActivity.this.day = dayOfMonth;
                showDateTime(year, month, day, hour, minute);
            }
        });

        mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                PickerDialogActivity.this.hour = hourOfDay;
                PickerDialogActivity.this.minute = minute;
                showDateTime(year, month, day, hour, minute);
            }
        });
    }

    private void showDateTime(int year, int month, int day, int hour, int minute) {
        mTextView.setText("Year:"+year+" Month:"+month+" Day:"+day+" Hour:"+hour+" Minute:"+minute);
    }

}

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".PickerDialogActivity">

    <TextView
        android:id="@+id/tv_picker_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <DatePicker
        android:id="@+id/picker_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:startYear="2000"
        android:endYear="2018"
        android:calendarViewShown="true"
        android:spinnersShown="true"
        />
    <TimePicker
        android:id="@+id/picker_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

运行效果:


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页