安卓学习第37课——DatePickerDialog、TimePickerDialog

<?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"
    android:gravity="center_horizontal"
    >
<EditText 
    android:id="@+id/show"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="none"
    />
<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    >
<Button
    android:id="@+id/dateBn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置日期"
    />
<Button
    android:id="@+id/timeBn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="设置时间"
    />    
</LinearLayout>
</LinearLayout>
package com.example.datetimedialog;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button dateBn=(Button) findViewById(R.id.dateBn);
        Button timeBn=(Button) findViewById(R.id.timeBn);
        dateBn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Calendar c=Calendar.getInstance();
                new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                    
                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        EditText show=(EditText) findViewById(R.id.show);
                        show.setText("您选择了"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日");
                        
                    }
                }
                ,c.get(Calendar.YEAR)
                , c.get(Calendar.MONTH)
                , c.get(Calendar.DAY_OF_MONTH)).show();
            }
            
        });
        timeBn.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                Calendar c=Calendar.getInstance();
                new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {

                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay,
                            int minute) {
                        EditText show=(EditText) findViewById(R.id.show);
                        show.setText("您选择了"+hourOfDay+"时"+minute+"分");
                    }
                }
                ,c.get(Calendar.HOUR_OF_DAY)
                , c.get(Calendar.MINUTE)
                , true).show();
            }
            
        });
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。