代码改变世界

DatePicker输入日期控件的使用

2014-03-30 15:32  kingshow  阅读(2123)  评论(0编辑  收藏  举报

 

   DataPicker(输入日期控件),DatePicker控件可用于输入日期,日期的范围是1900-1-1到2100-12-31.

一、建立工程,如图

二、activity_main.xml中代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <DatePicker
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/datepicker"
        />
    <TimePicker 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/timepicker"
        />
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textview"
        android:textSize="18dp"
        
        />
    
</LinearLayout>
View Code

三、MainActivity.java中代码

package com.study.datetimepicker;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.TimePicker.OnTimeChangedListener;

public class MainActivity extends Activity implements OnDateChangedListener,OnTimeChangedListener{

    private TextView textView;
    private DatePicker datePicker;
    private TimePicker timePicker;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        datePicker = (DatePicker)this.findViewById(R.id.datepicker);
        timePicker = (TimePicker)this.findViewById(R.id.timepicker);
        textView = (TextView)this.findViewById(R.id.textview);
        datePicker.init(2001, 1, 25, this);//初始化日期
        timePicker.setIs24HourView(true);//显示时间是否是按24小时制
        timePicker.setOnTimeChangedListener(this);//注册事件
        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //日期控件的触发
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
                timePicker.getCurrentHour(), timePicker.getCurrentMinute());
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm");
        textView.setText(format.format(calendar.getTime()));
        
    }

    //时间空间的触发
    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        Toast.makeText(MainActivity.this, "hourOfDay:" + hourOfDay + "  minute:" + minute, 1).show();
        
        
    }
}
View Code

四、效果图