DatePicker和TimePicker

package com.example.administrator.mytest;

import android.annotation.TargetApi;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class Main22Activity extends AppCompatActivity {
private Button bt_date,bt_time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main22);
        bt_date= (Button) findViewById(R.id.bt_date);
        bt_time= (Button) findViewById(R.id.bt_time);
        bt_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDate(v);
            }
        });
        bt_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTime(v);
            }
        });

    }
    @TargetApi(Build.VERSION_CODES.N)
    public void showDate(View view){
        Calendar calendar=Calendar.getInstance();

        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                Toast.makeText(Main22Activity.this, "您选择的日期为:"+year+":"+(month+1)+":"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        },calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    public void showTime(View view){

        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(Main22Activity.this, "您选择的时间为:"+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
            }
        },12,12,true).show();

    }
}
<?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="horizontal"
    tools:context="com.example.administrator.mytest.Main22Activity">
    <Button
        android:id="@+id/bt_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日期按钮"/>
    <Button
        android:id="@+id/bt_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="时间按钮"/>


</LinearLayout>

posted @ 2016-10-14 14:09  什么玩楞啊,我叫旺仔  阅读(217)  评论(0编辑  收藏  举报