Android開發(fā)之時(shí)間日期操作實(shí)例
相信對于手機(jī)的時(shí)間日期設(shè)置大家一定都不陌生吧,今天舉一個(gè)關(guān)于時(shí)間日期設(shè)置的示例,其中有些許不完善之處,例如如何使設(shè)置的時(shí)間日期和手機(jī)系統(tǒng)同步等。感興趣的讀者可以根據(jù)自身經(jīng)驗(yàn)加以完善。
現(xiàn)來看看具體示例,希望對大家有所幫助。
首先是時(shí)間設(shè)置:
.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="設(shè)置時(shí)間" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下一頁" /> </LinearLayout> </RelativeLayout>
運(yùn)行效果如下圖所示:
日期設(shè)置和時(shí)間設(shè)置基本一致,在此不再贅述。讀者可以調(diào)試并改進(jìn)本文示例代碼,相信會(huì)有新的收獲!
- 解析android中系統(tǒng)日期時(shí)間的獲取
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android中日期與時(shí)間設(shè)置控件用法實(shí)例
- Android日期時(shí)間格式國際化的實(shí)現(xiàn)代碼
- Android開發(fā)之時(shí)間日期組件用法實(shí)例
- Android之日期及時(shí)間選擇對話框用法實(shí)例分析
- Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼
- Android仿iPhone日期時(shí)間選擇器詳解
- Android開發(fā)中DatePicker日期與時(shí)間控件實(shí)例代碼
- Android 日期和時(shí)間的使用實(shí)例詳解
- Android開發(fā)獲取當(dāng)前系統(tǒng)日期和時(shí)間功能示例
相關(guān)文章
Android自定義控件通用驗(yàn)證碼輸入框的實(shí)現(xiàn)
這篇文章主要介紹了Android自定義控件通用驗(yàn)證碼輸入框的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼
這篇文章主要介紹了Android Listview中顯示不同的視圖布局詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法示例
這篇文章主要介紹了Android TextView顯示Html類解析的網(wǎng)頁和圖片及自定義標(biāo)簽用法,實(shí)例分析了Android中TextView控件的使用技巧,需要的朋友可以參考下2016-07-07Android程序打開和對輸入法的操作(打開/關(guān)閉)
整理了一下Android下對輸入法的操作:打開輸入法窗口、關(guān)閉出入法窗口、如果輸入法打開則關(guān)閉,如果沒打開則打開、獲取輸入法打開的狀態(tài)2013-05-05Cocos2d-x入門教程(詳細(xì)的實(shí)例和講解)
這篇文章主要介紹了Cocos2d-x入門教程,包括詳細(xì)的實(shí)例、講解以及實(shí)現(xiàn)過程,需要的朋友可以參考下2014-04-04Flutter調(diào)用Android和iOS原生代碼的方法示例
這篇文章主要給大家介紹了關(guān)于Flutter調(diào)用Android和iOS原生代碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04巧用ViewPager實(shí)現(xiàn)駕考寶典做題翻頁效果
本文主要介紹巧用ViewPager實(shí)現(xiàn)駕考寶典做題翻頁效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03