Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 開發之時間日期操作實例

編輯:Android 開發實例

對於手機的時間日期設置,大家應該都不陌生。今天就用一個時間日期設置的示例來做說明,其中尚有可完善之處,例如如何讓設置的時間日期與手機系統同步等,有興趣的讀者可以結合自身經驗加以改進。

先來看具體示例,希望能對大家有所幫助。首先是時間設置:

.java 文件(MainActivity.java)代碼如下:

 package com.example.activity_time_date; import java.util.Calendar; import android.app.Activity; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.TimePicker; public class MainActivity extends Activity { private TextView mytext = null; private Button mybutton1 = null; private Button mybutton2 = null; private int mHour; private int mMinute; static final int TIME_DIALOG_ID = 0; private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub mHour = hourOfDay; mMinute = minute; updateDisplay(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mytext = (TextView)findViewById(R.id.textview); mybutton1 = (Button)findViewById(R.id.button1); mybutton2 = (Button)findViewById(R.id.button2); mybutton1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showDialog(TIME_DIALOG_ID); } }); mybutton2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MainActivity.this.startActivity(new Intent(MainActivity.this, dateActivity.class)); } }); final Calendar cal = Calendar.getInstance(); mHour = cal.get(Calendar.HOUR_OF_DAY); mMinute = cal.get(Calendar.MINUTE); updateDisplay(); } private void updateDisplay(){ mytext.setText(new StringBuilder().append(pad(mHour)).append(":") .append(pad(mMinute))); } private static String pad(int i){ if (i >= 10) return String.valueOf(i); else return "0" + String.valueOf(i); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case TIME_DIALOG_ID: return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false); } return null; } } 

布局文件(activity_main.xml)代碼如下:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="設置時間" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下一頁" /> </LinearLayout> </RelativeLayout> 

運行效果如下圖所示:

日期設置與時間設置的思路基本一致——把 TimePickerDialog 換成 DatePickerDialog 即可,這裡不再贅述。讀者可以調試並改進本文的示例代碼,相信會有新的收獲!

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved