Android之日期及時(shí)間選擇對話框用法實(shí)例分析
本文實(shí)例講述了Android之日期及時(shí)間選擇對話框用法。分享給大家供大家參考。具體如下:
清單文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ljq.dialog" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".AlertDialog" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="android.permission.WRITE_CALENDAR" /> </manifest>
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" /> <Button android:text="日期對話框" android:id="@+id/dateBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:text="時(shí)間對話框" android:id="@+id/timeBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <DigitalClock android:text="@+id/digitalClock" android:textSize="20dip" android:gravity="center" android:id="@+id/DigitalClock01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <AnalogClock android:id="@+id/analogClock" android:gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
AlertActivity類:
package com.ljq.dialog; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker; public class AlertDialog extends Activity { private Button dateBtn = null; private Button timeBtn = null; private EditText et=null; private final static int DATE_DIALOG = 0; private final static int TIME_DIALOG = 1; private Calendar c = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et=(EditText)findViewById(R.id.et); dateBtn = (Button) findViewById(R.id.dateBtn); timeBtn = (Button) findViewById(R.id.timeBtn); dateBtn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { showDialog(DATE_DIALOG); } }); timeBtn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { showDialog(TIME_DIALOG); } }); } /** * 創(chuàng)建日期及時(shí)間選擇對話框 */ @Override protected Dialog onCreateDialog(int id) { Dialog dialog = null; switch (id) { case DATE_DIALOG: c = Calendar.getInstance(); dialog = new DatePickerDialog( this, new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) { et.setText("您選擇了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日"); } }, c.get(Calendar.YEAR), // 傳入年份 c.get(Calendar.MONTH), // 傳入月份 c.get(Calendar.DAY_OF_MONTH) // 傳入天數(shù) ); break; case TIME_DIALOG: c=Calendar.getInstance(); dialog=new TimePickerDialog( this, new TimePickerDialog.OnTimeSetListener(){ public void onTimeSet(TimePicker view, int hourOfDay, int minute) { et.setText("您選擇了:"+hourOfDay+"時(shí)"+minute+"分"); } }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), false ); break; } return dialog; } }
運(yùn)行結(jié)果:
希望本文所述對大家的Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)日期時(shí)間選擇對話框
- Android 自定義日期段選擇控件功能(開始時(shí)間-結(jié)束時(shí)間)
- Android開發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時(shí)間功能示例
- Android仿iPhone日期時(shí)間選擇器詳解
- Android日期和時(shí)間選擇器實(shí)現(xiàn)代碼
- Android之日期時(shí)間選擇控件DatePicker和TimePicker實(shí)例
- Android開發(fā)中實(shí)現(xiàn)IOS風(fēng)格底部選擇器(支持時(shí)間 日期 自定義)
- Android時(shí)間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android中TimePicker與DatePicker時(shí)間日期選擇組件的使用實(shí)例
- Android開發(fā)實(shí)現(xiàn)日期時(shí)間控件選擇
相關(guān)文章
Android實(shí)現(xiàn)QQ側(cè)滑菜單效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)QQ側(cè)滑菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android利用Flutter實(shí)現(xiàn)立體旋轉(zhuǎn)效果
本文主要介紹了Flutter繪圖如何使用ImageShader填充圖形,并且利用 Matrix4的三維變換加上動畫實(shí)現(xiàn)了立體旋轉(zhuǎn)的動畫效果,感興趣的可以嘗試一下2022-06-06Android開發(fā)之React Navigation 導(dǎo)航欄樣式調(diào)整+底部角標(biāo)消息提示
這篇文章主要介紹了React Navigation 導(dǎo)航欄樣式調(diào)整+底部角標(biāo)消息提示的相關(guān)知識,非常不錯,具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例
這篇文章主要介紹了Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android左滑返回功能的實(shí)現(xiàn)示例代碼
本篇文章主要介紹了Android左滑返回的實(shí)現(xiàn)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07android為ListView每個Item上面的按鈕添加事件
本篇文章主要介紹了android為ListView每個Item上面的按鈕添加事件,有興趣的同學(xué)可以了解一下。2016-11-11詳解如何使用Android Studio 進(jìn)行NDK開發(fā)和調(diào)試
本篇文章主要介紹了詳解如何使用Android Studio 進(jìn)行NDK開發(fā)和調(diào)試,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12