欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中日期與時間設置控件用法實例

 更新時間:2015年07月14日 17:52:51   作者:鑒客  
這篇文章主要介紹了Android中日期與時間設置控件用法,實例分析了Android日期與時間相關控件的相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Android中日期與時間設置控件用法。分享給大家供大家參考。具體如下:

1、日期設置控件:DatePickerDialog

2、時間設置控件:TimePickerDialog

實例代碼:

頁面添加兩個Button,單擊分別顯示日期設置控件和時間設置控件,還是有TextView控件,用于顯示設置后的系統(tǒng)時間

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
 Android:orientation="vertical"
 Android:layout_width="fill_parent"
 Android:layout_height="fill_parent"
 >
<TextView Android:id="@+id/dateAndTime"
 Android:layout_width="fill_parent"
 Android:layout_height="wrap_content"
 Android:text="@string/hello"
 />
<Button
 Android:id="@+id/setDate"
 Android:layout_width="fill_parent"
 Android:layout_height="wrap_content"
 Android:text="Set the Date"></Button>
<Button Android:id="@+id/setTime"
 Android:layout_width="fill_parent"
 Android:layout_height="wrap_content"
 Android:text="Set the Time"></Button>
</LinearLayout>

ChronoDemo.java如下:

package yyl.Android;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Locale;
import Android.app.Activity;
import Android.app.DatePickerDialog;
import Android.app.TimePickerDialog;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.Button;
import Android.widget.DatePicker;
import Android.widget.TextView;
import Android.widget.TimePicker;
public class ChronoDemo extends Activity {
 //獲取日期格式器對象
 DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
 //定義一個TextView控件對象
 TextView dateAndTimeLabel = 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的顯示更新為最新時間
  updateLabel();  
 } 
 };
 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);
  updateLabel();
 }
 };
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 //得到頁面設定日期的按鈕控件對象
 Button dateBtn = (Button)findViewById(R.id.setDate);
 //設置按鈕的點擊事件監(jiān)聽器
 dateBtn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  //生成一個DatePickerDialog對象,并顯示。顯示的DatePickerDialog控件可以選擇年月日,并設置
  new DatePickerDialog(ChronoDemo.this,
   d,
   dateAndTime.get(Calendar.YEAR),
   dateAndTime.get(Calendar.MONTH),
   dateAndTime.get(Calendar.DAY_OF_MONTH)).show();
  }
 });
 Button timeBtn = (Button)findViewById(R.id.setTime);
 timeBtn.setOnClickListener(new View.OnClickListener() {
  //同上原理
  @Override
  public void onClick(View v) {
  new TimePickerDialog(ChronoDemo.this,
   t,
   dateAndTime.get(Calendar.HOUR_OF_DAY),
   dateAndTime.get(Calendar.MINUTE),
   true).show();
  }
 });
 dateAndTimeLabel=(TextView)findViewById(R.id.dateAndTime);
 updateLabel();
 }
 //更新頁面TextView的方法
 private void updateLabel() {
 dateAndTimeLabel.setText(fmtDateAndTime
 .format(dateAndTime.getTime()));
 }
}

希望本文所述對大家的Android程序設計有所幫助。

相關文章

最新評論