随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

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);
}
});

}

/**
* 创建日期及时间选择对话框
*/
@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) // 传入天数
);
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;
}

}
复制代码

运行结果

1、2、

posted on   Ruthless  阅读(39278)  评论(2编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
< 2011年3月 >
27 28 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 1 2
3 4 5 6 7 8 9

点击右上角即可分享
微信分享提示