Android之日期及时间选择对话框用法实例分析

 更新时间:2015年09月14日 10:48:16   作者:Ruthless  
这篇文章主要介绍了Android之日期及时间选择对话框用法,以实例形式较为详细的分析了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程序设计有所帮助。

相关文章

  • Android使用Jsoup解析Html表格的方法

    Android使用Jsoup解析Html表格的方法

    这篇文章主要介绍了Android使用Jsoup解析Html表格的方法,涉及Android中Jsoup的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-12-12
  • Android实现旋转,放大,缩小图片的方法

    Android实现旋转,放大,缩小图片的方法

    这篇文章主要介绍了Android实现旋转,放大,缩小图片的方法,结合实例形式分析了Android基于Drawable针对图片的缩放与旋转等处理技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-10-10
  • Android自定义View——扇形统计图的实现代码

    Android自定义View——扇形统计图的实现代码

    本篇文章主要介绍了Android自定义View——扇形统计图的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • 简单实现android短信发送器

    简单实现android短信发送器

    这篇文章主要为大家详细介绍了如何简单实现android短信发送器 ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android DialogUtils弹出窗工具类详解

    Android DialogUtils弹出窗工具类详解

    这篇文章主要为大家详细介绍了Android DialogUtils弹出窗工具类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • Android 实现带头部文字输入框的自定义控件

    Android 实现带头部文字输入框的自定义控件

    这篇文章主要介绍了Android 实现带头部文字输入框的自定义控件,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-04-04
  • android打开rar压缩文件

    android打开rar压缩文件

    这篇文章主要介绍了android打开rar压缩文件示例,调用RAR for android 打开压缩文件,需要的朋友可以参考下
    2014-03-03
  • android编程实现系统图片剪裁的方法

    android编程实现系统图片剪裁的方法

    这篇文章主要介绍了android编程实现系统图片剪裁的方法,涉及Android针对图片的获取、修改、保存等操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • Android中实现下载和解压zip文件功能代码分享

    Android中实现下载和解压zip文件功能代码分享

    这篇文章主要介绍了Android中实现下载和解压zip文件功能代码分享,本文直接给出了实现代码,需要的朋友可以参考下
    2015-03-03
  • Android实现淘宝选中商品尺寸的按钮组实例

    Android实现淘宝选中商品尺寸的按钮组实例

    这篇文章介绍的是仿淘宝中的选中商品不同尺寸,比如衣服有L、M、XL等等的款式。这时候我们就需要一个button组来进行了,当时这个时候里面的尺寸可能有很多,那怎么办呢?这里我们就肯定要做个自适应的按钮组了,要不然弄出来也没用。
    2016-08-08

最新评论