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

Android中日期與時(shí)間設(shè)置控件用法實(shí)例

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

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

1、日期設(shè)置控件:DatePickerDialog

2、時(shí)間設(shè)置控件:TimePickerDialog

實(shí)例代碼:

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

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 {
 //獲取日期格式器對(duì)象
 DateFormat fmtDateAndTime = DateFormat.getDateTimeInstance();
 //定義一個(gè)TextView控件對(duì)象
 TextView dateAndTimeLabel = null;
 //獲取一個(gè)日歷對(duì)象
 Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);
 //當(dāng)點(diǎn)擊DatePickerDialog控件的設(shè)置按鈕時(shí),調(diào)用該方法
 DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener()
 {
 @Override
 public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
  //修改日歷控件的年,月,日
  //這里的year,monthOfYear,dayOfMonth的值與DatePickerDialog控件設(shè)置的最新值一致
  dateAndTime.set(Calendar.YEAR, year);
  dateAndTime.set(Calendar.MONTH, monthOfYear);
  dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
  //將頁面TextView的顯示更新為最新時(shí)間
  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);
 //得到頁面設(shè)定日期的按鈕控件對(duì)象
 Button dateBtn = (Button)findViewById(R.id.setDate);
 //設(shè)置按鈕的點(diǎn)擊事件監(jiān)聽器
 dateBtn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  //生成一個(gè)DatePickerDialog對(duì)象,并顯示。顯示的DatePickerDialog控件可以選擇年月日,并設(shè)置
  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()));
 }
}

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論