android--日期时间控件(十二)

一、先看看实现的效果:

技术分享

点击日期"2015-03-24"会弹出选择日期的对话框

技术分享

点击时间,则弹出时间的对话框

技术分享

二、代码如下:

1.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  android:id="@+id/txtDate"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="点击设置日期"
    />

<TextView  android:id="@+id/txtTime"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="2dp"
    android:text="点击设置时间"
    />

</LinearLayout>

2.后台代码如下

package com.example.time;

import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.util.Log;
import android.view.Menu;
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 {

	private final static String TAG="TimeDate";
	   //获取日期格式器对象
    DateFormat fmtDate = new java.text.SimpleDateFormat("yyyy-MM-dd");
    
    DateFormat fmtTime = new java.text.SimpleDateFormat("HH:mm:ss");
    
    //定义一个TextView控件对象
    TextView txtDate = null;
    TextView txtTime = null;
    //获取一个日历对象
    Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);
    
    
    //当点击DatePickerDialog控件的设置按钮时,调用该方法
    DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener()
    {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            //修改日历控件的年,月,日
            //这里的year,monthOfYear,dayOfMonth的值与DatePickerDialog控件设置的最新值一致
            dateAndTime.set(Calendar.YEAR, year);
            dateAndTime.set(Calendar.MONTH, monthOfYear);
            dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);    
            //将页面TextView的显示更新为最新时间
            upDateDate();   
            
        }        
    };
    
    TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
        
        //同DatePickerDialog控件
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            dateAndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            dateAndTime.set(Calendar.MINUTE, minute);
            upDateTime();
            
        }
    }; 
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Log.d(TAG,"onCreate");
		txtDate =(TextView)findViewById(R.id.txtDate);
		txtDate.setClickable(true);  
		txtDate.setFocusable(true);
		txtTime =(TextView)findViewById(R.id.txtTime);
		txtTime.setClickable(true);  
		txtTime.setFocusable(true);
		
		txtDate.setOnClickListener(new OnClickListener(){  
		        @Override  
		        public void onClick(View v){  
		            Log.d(TAG,"txtDate click start");  
		            DatePickerDialog  dateDlg = new DatePickerDialog(MainActivity.this,
		                    d,
		                    dateAndTime.get(Calendar.YEAR),
		                    dateAndTime.get(Calendar.MONTH),
		                    dateAndTime.get(Calendar.DAY_OF_MONTH));
		         
		            dateDlg.show();
		            
		            Log.d(TAG,"Date show");
		     }
		   });
		
		txtTime.setOnClickListener(new OnClickListener(){  
	        @Override  
	        public void onClick(View v){  
	            Log.d(TAG,"txtTime click start"); 
	            Log.d(TAG,"Date show");
	            TimePickerDialog timeDlg = new TimePickerDialog(MainActivity.this,
	                    t,
	                    dateAndTime.get(Calendar.HOUR_OF_DAY),
	                    dateAndTime.get(Calendar.MINUTE),
	                    true);
	            timeDlg.show();
	     }
	   });
		
		upDateDate();
		upDateTime();
	}
	
    private void upDateDate() {
    	txtDate.setText(fmtDate.format(dateAndTime.getTime()));
        }
   
    private void upDateTime() {
    	txtTime.setText(fmtTime.format(dateAndTime.getTime()));
        }


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



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