Android之日期及時間選擇對話框用法實(shí)例分析
更新時間:2015年09月14日 10:48:16 作者:Ruthless
這篇文章主要介紹了Android之日期及時間選擇對話框用法,以實(shí)例形式較為詳細(xì)的分析了Android創(chuàng)建日期及時間選擇對話框的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了Android之日期及時間選擇對話框用法。分享給大家供大家參考。具體如下:
清單文件:
<?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="時間對話框" 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)建日期及時間選擇對話框
*/
@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+"時"+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)日期時間選擇對話框
- Android 自定義日期段選擇控件功能(開始時間-結(jié)束時間)
- Android開發(fā)之DatePicker和TimePicker實(shí)現(xiàn)選擇日期時間功能示例
- Android仿iPhone日期時間選擇器詳解
- Android日期和時間選擇器實(shí)現(xiàn)代碼
- Android之日期時間選擇控件DatePicker和TimePicker實(shí)例
- Android開發(fā)中實(shí)現(xiàn)IOS風(fēng)格底部選擇器(支持時間 日期 自定義)
- Android時間選擇器、日期選擇器實(shí)現(xiàn)代碼
- Android中TimePicker與DatePicker時間日期選擇組件的使用實(shí)例
- Android開發(fā)實(shí)現(xiàn)日期時間控件選擇
相關(guān)文章
建造者模式_動力節(jié)點(diǎn)Java學(xué)院整理
建造者實(shí)現(xiàn)抽象類的所有未實(shí)現(xiàn)的方法,具體來說一般是兩項(xiàng)任務(wù),組建產(chǎn)品;返回組建好的產(chǎn)品2017-08-08
Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)下拉頂部圖片變大,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android Mms之:聯(lián)系人管理的應(yīng)用分析
本篇文章是對Android中的聯(lián)系人管理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android onbackpressed實(shí)現(xiàn)返回鍵的攔截和彈窗流程分析
很多網(wǎng)友不明白如何在Android平臺上捕獲Back鍵的事件,Back鍵是手機(jī)上的后退鍵,一般的軟件不捕獲相關(guān)信息可能導(dǎo)致你的程序被切換到后臺,而回到桌面的尷尬情況,在Android上有兩種方法來獲取該按鈕的事件2023-01-01

