Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android--日期時間控件

android--日期時間控件

編輯:關於Android編程

一、先看看實現的效果:

\

點擊日期2015-03-24會彈出選擇日期的對話框

\

點擊時間,則彈出時間的對話框

\

二、代碼如下:

1.xml文件

 

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;
	}

}


 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved