android 日期控件对话框

有时候,我用到的日期控件的时候,要用对话的形式体现出来的,下面开始吧:

 

1.创建一个android-time

2.包名:com.app

3.主activity的名字:MainActivity

 

4.布局文件:main.xml

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>
 

5.activity的代码

 

  package com.app;

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

import com.app.R;
import com.app.view.DatePickText;



import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends Activity {
	
	public static final int DATE = 0;
	public static final int TIME = 1;
	
	private final Calendar cal = Calendar
	.getInstance(Locale.SIMPLIFIED_CHINESE);
	private SimpleDateFormat df;
	
	private TextView edit ;
	private Button button1 ;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        df = new SimpleDateFormat();
        
        this.edit = (TextView) this.findViewById(R.id.tv) ;
      
        this.button1 = (Button) this.findViewById(R.id.button1) ;
     
		this.button1.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				buildDateOrTimeDialog(MainActivity.this) ;
			}
			
		}) ;
        
    }
    
    private void buildDateOrTimeDialog(Context context) {
		df = new SimpleDateFormat("HH:mm:SS");
		switch (444) {
		case DATE:
			date: new DatePickerDialog(context, listener,
					cal.get(Calendar.YEAR),

					cal.get(Calendar.MONTH),

					cal.get(Calendar.DAY_OF_MONTH)

			).show();
			break;

		case TIME:
			System.out.println("----------time---------------");
			new TimePickerDialog(context, timeListen,
					cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
					true).show();
			break;
		default:
			new DatePickerDialog(context, listener, cal.get(Calendar.YEAR),
			cal.get(Calendar.MONTH),
			cal.get(Calendar.DAY_OF_MONTH)

			).show();

		}

	}
    
    
    
    private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { //

		@Override
		public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {

			cal.set(Calendar.YEAR, arg1);
			cal.set(Calendar.MONTH, arg2);
			cal.set(Calendar.DAY_OF_MONTH, arg3);

			updateDate();

		}

	};
	
	
	
	
	private void updateDate() {

		edit.setText(df.format(cal.getTime()));

	}

	TimePickerDialog.OnTimeSetListener timeListen = new TimePickerDialog.OnTimeSetListener() {

		// 同DatePickerDialog控件

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
			cal.set(Calendar.MINUTE, minute);
			cal.set(Calendar.SECOND, cal.get(Calendar.SECOND));
			updateTimes();
		}

	};

	// 更新页面TextView的方法
	private void updateTimes() {

		edit.setText(df.format(cal.getTime()));
	}
	
    
}
 

 

 

  说明的:

 

 其中 SimpleDateFormat df = new SimpleDateFormat("HH:mm:SS"); //定义获取时间的样子

 

 

switch (444) {
		case DATE:
			date: new DatePickerDialog(context, listener,
					cal.get(Calendar.YEAR),

					cal.get(Calendar.MONTH),

					cal.get(Calendar.DAY_OF_MONTH)

			).show();
			break;

		case TIME:
			System.out.println("----------time---------------");
			new TimePickerDialog(context, timeListen,
					cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),
					true).show();
			break;
		default:
			new DatePickerDialog(context, listener, cal.get(Calendar.YEAR),
			cal.get(Calendar.MONTH),
			cal.get(Calendar.DAY_OF_MONTH)

			).show();

		}
 

   定义的对话框中,我们要不要显示时间,你可以更改switch(DATA||TIME)来选择你要的样子。

 

 

  如果你还是看不懂上面的代码,或者用起来有问题,那么你可以联系:qq 1019990976。

 

  觉得是无图有真相的。

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值