日期对话框

<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="vertical"
     >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false"
        android:cursorVisible="false"
        ></EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="日期对话框" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="时间对话框" />
    <DigitalClock 
        android:id="@+id/digitalclock"
        android:textSize="20dip"
        android:gravity="center"
        android:text="DigitalClock"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
        <AnalogClock 
         android:id="@+id/analogclock"   
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
            />

</LinearLayout>

“`
package com.example.test;

import java.util.Calendar;

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

public class MainActivity extends Activity {
private Button datebtn=null;
private Button timebtn=null;
private EditText et=null;
private final static int DATE_DIALOG=0;
private final static int TIME_DIALOG=1;
private Calendar c=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.editText1);
datebtn=(Button) findViewById(R.id.button1);
timebtn=(Button) findViewById(R.id.button2);
datebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            showDialog(DATE_DIALOG);
        }
    });
    timebtn.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            showDialog(TIME_DIALOG);
        }
    });
}
@Override     
protected Dialog onCreateDialog(int id) {          
    Dialog dialog = null;          
    switch (id) {          
       case DATE_DIALOG:              
           c = Calendar.getInstance();              
           dialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {  
               public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {  
                   et.setText("您选择了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日");   
               }   
           }, c.get(Calendar.YEAR), // 传入年份                
           c.get(Calendar.MONTH), // 传入月份                 
           c.get(Calendar.DAY_OF_MONTH) // 传入天数            
           );              
           break;          
       case TIME_DIALOG:              
           c=Calendar.getInstance();              
           dialog=new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener(){ 
               @Override
               public void onTimeSet(TimePicker view, int hourOfDay, int minute) {    
                   et.setText("您选择了:"+hourOfDay+"时"+minute+"分");                   
               }  
           },  
           c.get(Calendar.HOUR_OF_DAY),  
           c.get(Calendar.MINUTE),  
           false  
           );  
           break;          
       }          
       return dialog;      
    }  

}

“`这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值