Android之日期及時間選擇對話框用法實例分析
更新時間:2015年09月14日 10:48:16 作者:Ruthless
這篇文章主要介紹了Android之日期及時間選擇對話框用法,以實例形式較為詳細的分析了Android創(chuàng)建日期及時間選擇對話框的實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了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;
}
}
運行結(jié)果:


希望本文所述對大家的Android程序設(shè)計有所幫助。
您可能感興趣的文章:
- Android實現(xiàn)日期時間選擇對話框
- Android 自定義日期段選擇控件功能(開始時間-結(jié)束時間)
- Android開發(fā)之DatePicker和TimePicker實現(xiàn)選擇日期時間功能示例
- Android仿iPhone日期時間選擇器詳解
- Android日期和時間選擇器實現(xiàn)代碼
- Android之日期時間選擇控件DatePicker和TimePicker實例
- Android開發(fā)中實現(xiàn)IOS風格底部選擇器(支持時間 日期 自定義)
- Android時間選擇器、日期選擇器實現(xiàn)代碼
- Android中TimePicker與DatePicker時間日期選擇組件的使用實例
- Android開發(fā)實現(xiàn)日期時間控件選擇
相關(guān)文章
Android利用Flutter實現(xiàn)立體旋轉(zhuǎn)效果
本文主要介紹了Flutter繪圖如何使用ImageShader填充圖形,并且利用 Matrix4的三維變換加上動畫實現(xiàn)了立體旋轉(zhuǎn)的動畫效果,感興趣的可以嘗試一下2022-06-06
Android開發(fā)之React Navigation 導航欄樣式調(diào)整+底部角標消息提示
這篇文章主要介紹了React Navigation 導航欄樣式調(diào)整+底部角標消息提示的相關(guān)知識,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05
android為ListView每個Item上面的按鈕添加事件
本篇文章主要介紹了android為ListView每個Item上面的按鈕添加事件,有興趣的同學可以了解一下。2016-11-11
詳解如何使用Android Studio 進行NDK開發(fā)和調(diào)試
本篇文章主要介紹了詳解如何使用Android Studio 進行NDK開發(fā)和調(diào)試,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

