2.4.3 日期/时间选择器

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <DatePicker
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

    <TimePicker
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="10pt"
        android:text="TextView" />

</LinearLayout>
package com.moon.hello2;

import java.util.Calendar;

import android.R.anim;
import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class MainActivity extends Activity {
    private int year;
    private int month;
    private int day;
    private int hour;
    private int minute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        DatePicker datePicker = (DatePicker)findViewById(R.id.date);
        TimePicker timePicker = (TimePicker)findViewById(R.id.time);
        Calendar calendar = Calendar.getInstance();
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),new OnDateChangedListener(){

            @Override
            public void onDateChanged(DatePicker date, int year, int month,
                    int day) {
                // TODO Auto-generated method stub
                MainActivity.this.year = year;
                MainActivity.this.month = month;
                MainActivity.this.day = day;
                showDate();
            }
            
        });
        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
            
            @Override
            public void onTimeChanged(TimePicker arg0, int hour, int minute) {
                // TODO Auto-generated method stub
                MainActivity.this.hour = hour;
                MainActivity.this.minute = minute;
                showDate();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private void showDate(){
        TextView textView = (TextView)findViewById(R.id.textView1);
        textView.setText(year+"年"+month+"月"+day+"日"+hour+"小时"+minute+"分");
    }

}

 

posted @ 2015-05-14 17:22  魔芋红茶  阅读(123)  评论(0编辑  收藏  举报