当前位置:首页 > Android > 正文内容

Android之日期及时间选择对话框

jsc10年前 (2016-04-06)Android2957

清单文件

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

}

运行结果


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/81.html

分享给朋友:

“Android之日期及时间选择对话框” 的相关文章

Android基本功:支持GPS的核心API

一、LocationManager类        作用和TelephonyManager,AudioManager等服务类的作用类似,所有GPS定位相关的服务、对象都由该对象产生;    ...

使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果。

使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果。

在工作中又很多需求都不是android系统自带的控件可以达到效果的,内置的TabHost就是,只能达到简单的效果 ,所以这个时候就要自定义控件来达到效果:这个效果就是: 使用自定义RadioButton和ViewPager实现TabHost带滑动的页卡效果。    &...

google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果

    了解二维码这个东西还是从微信中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候,老板说要加上二维码扫描功能,然后自己的屁颠屁颠的去百度,google...

Android中从SD卡中/拍照选择图片并进行剪裁的方法

Android中从SD卡中/拍照选择图片并进行剪裁的方法

效果图: 下面是代码的部分,部分是从网路上摘录的,自己整理后当做工具类使用   配置文件:布局很简单,一个ImageButton和一个Button,点击都可以实现图像选择的功能,具体的实现根据大家在实际中...

Android 更换皮肤思路及解决方案

Android 更换皮肤思路及解决方案

本篇博客要给大家分享的一个关于Android应用换肤的Demo,大家可以到我的github去下载demo,以后博文涉及到的代码均会上传到github中统一管理。 github地址:https://github.com/devilWwj/Android-skin-upda...

Fragment保持状态切换

Fragment保持状态切换

在使用Activity管理多个Fragment时,每次切换Fragment使用的是replace,结果导致出现xxx is not currently in the FragmentManager异常挂掉网上说使用replace切换会使被切换的Fragment给替换掉,从而被被切换的Fra...