Android之日期及时间选择对话框用法实例分析
作者:Ruthless 发布时间:2023-06-26 23:59:34
标签:Android,日期,时间,对话框
本文实例讲述了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;
}
}
运行结果:
希望本文所述对大家的Android程序设计有所帮助。


猜你喜欢
- springMVC后台处理数组对象。list类型的参数,接收前台的数组值,实验了一下,结果还真可以。不用绑定到对象里面。当然我这个是前台传递
- 在这篇文章中,我们将介绍如下内容:==运算符与基元类型==运算符与引用类型==运算符与String类型==运算符与值类型==运算符与泛型==
- 本文实例为大家分享了java实现滑动验证解锁的具体代码,供大家参考,具体内容如下1.html:<div class="dra
- 这里用到一个HTML解析辅助类:HtmlAgilityPack,如果没有网上找一个增加到库里,这个插件有很多版本,如果你开发环境是使用VS2
- 如下所示:@Overridepublic void onWindowFocusChanged(boolean hasFocus) {supe
- 一、自定义Dialog在沉浸式效果下,当界面弹出对话框时,对话框将获取到焦点,这将导致界面退出沉浸式效果,那么是不是能通过屏蔽对话框获取焦点
- 这篇文章主要介绍了Java调用明华RF读写器DLL文件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 问题描述我在接受 mq 消息的时候,需要做一个重试次数限制,如果超过 maxNum 就发邮件告警,不再重试。所以我需要对 consumer
- View绘制的三大流程,指的是measure(测量)、layout(布局)、draw(绘制) measure负责确定View的测量宽/高,也
- 报错信息:java.lang.Exception: org.apache.hadoop.mapreduce.task.reduce.Shuf
- 关于Retrofit的学习,我算是比较晚的了,而现在Retrofit已经是Android非常流行的网络请求框架了。之前,我没有学过Retro
- 背景在工作中,遇到这样的场景:有个es索引构建服务,需要从各个业务服务获取索引的信息,从而构建索引,业务服务都实现同一个接口&mda
- 本文为大家分享了自己写的一个Pager分页组件,WebForm,Mvc都适用,具体内容如下分页控件其实就是根据链接在页面间传递参数,因为我看
- 本文为大家分享了10道springboot常见面试题,供大家参考,具体内容如下1.什么是Spring Boot?多年来,随着新功能的增加,s
- 一、前言前面我们学习了多态中的转型,那么现在我们开始学习抽象类的概述和使用二、抽象类生活大多事物是具有抽象含义的,比如我说一个生物,你想不到
- java 值Document解析xml详细介绍使用jar包:jdom.jar配置文件格式 global.xml一、获取输入的值组成的结点我们
- IntelliJ IDEA是广受Java开发者喜爱的工具,其商业版的价格十分昂贵,如下图:现在有机会免费获取IntelliJ IDEA的正版
- 前言Java8 的新特性:Lambda表达式、强大的 Stream API、全新时间日期 API、ConcurrentHashMap、Met
- 1.下载安装OpenCVhttps://opencv.org/releases/选择合适的平台安装包下载,然后双击安装,也就是解压的过程。这
- 最近做一个小玩意需要识别验证码,之前从来没有接触过识别验证码这块,这可难倒了我。所以,在网上搜索如何识别验证码,许多前辈写的博文教会了我。怕