Android開發之時間日期操作執行個體_Android

來源:互聯網
上載者:User

相信對於手機的時間日期設定大家一定都不陌生吧,今天舉一個關於時間日期設定的樣本,其中有些許不完善之處,例如如何使設定的時間日期和手機系統同步等。感興趣的讀者可以根據自身經驗加以完善。

現來看看具體樣本,希望對大家有所協助。

首先是時間設定:

.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="http://schemas.android.com/apk/res/android" xmlns:tools="http://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>  

運行效果如下圖所示:

日期設定和時間設定基本一致,在此不再贅述。讀者可以調試並改進本文範例程式碼,相信會有新的收穫!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.