Android的DatePicker和TimePicker-android学习之旅(三十八)

Posted wzjhoutai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android的DatePicker和TimePicker-android学习之旅(三十八)相关的知识,希望对你有一定的参考价值。

DatePicker和TimePicker简单介绍

DatePicker和TimePicker是从FrameLayout继承而来。他们都是比較简单的组件。时间改变时间分别加入OnDateChangeListener()和OnTimeChangeListener()来监听。

DatePicker和TimePicker属性

技术分享

选择日期和时间的实例

package peng.liu.test;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends Activity {
    private DatePicker datePicker;
    TimePicker timePicker;
    EditText edit;
    private int year,month,day,hour,minute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        datePicker = (DatePicker) findViewById(R.id.datePicker);
        timePicker = (TimePicker) findViewById(R.id.timePicker);
        edit = (EditText) findViewById(R.id.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);
        minute = c.get(Calendar.MINUTE);
        datePicker.init(year,month,day,new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker datePicker, int i, int i2, int i3) {
                year = i;
                month = i2;
                day = i3;
                showDate(year,month,day,hour,minute);
            }
        });
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int i, int i2) {
                hour = i;
                minute = i2;
                showDate(year,month,day,hour,minute);
            }
        });
    }
    private void showDate(int year,int month,int day,int hour,int minute){
        edit.setText(year+month+day+hour+day+"");
    }
}
<?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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
    android:orientation="vertical"
        >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="选择详细的时间"/>
    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:id="@+id/datePicker"
        android:startYear="2000"
        android:endYear="2015"
        android:layout_gravity="center_horizontal"
        android:spinnersShown="true"
        android:calendarViewShown="true"/>
    <TimePicker
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:id="@+id/timePicker"
        android:layout_gravity="center_horizontal"/>
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/show"
        android:editable="false"
        android:cursorVisible="false"/>
</LinearLayout>

效果图

技术分享

以上是关于Android的DatePicker和TimePicker-android学习之旅(三十八)的主要内容,如果未能解决你的问题,请参考以下文章

Android中Dialog

Android - 新的日历样式 DatePicker 和 TimePicker

如何更改 Android 中 DatePicker 和 TimePicker 对话框的默认颜色?

Android DatePicker 和 Java 的 Date

Android中关闭DatePicker和NumberPicker等Picker类的可编辑模式

Android零基础入门第57节:日期选择器DatePicker和时间选择器TimePicker