有时候,我用到的日期控件的时候,要用对话的形式体现出来的,下面开始吧:

1.创建一个android-time

2.包名:com.app

3.主activity的名字:MainActivity

4.布局文件:main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/tv"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Button" /></LinearLayout>

5.activity的代码

package com.app;

import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Locale;import com.app.R;import com.app.view.DatePickText;import android.app.Activity;import android.app.DatePickerDialog;import android.app.Dialog;import android.app.TimePickerDialog;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.DatePicker;import android.widget.TextView;import android.widget.TimePicker;public class MainActivity extends Activity {public static final int DATE = 0;public static final int TIME = 1;private final Calendar cal = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);private SimpleDateFormat df;private TextView edit ;private Button button1 ;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        df = new SimpleDateFormat();                this.edit = (TextView) this.findViewById(R.id.tv) ;              this.button1 = (Button) this.findViewById(R.id.button1) ;     this.button1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubbuildDateOrTimeDialog(MainActivity.this) ;}}) ;            }        private void buildDateOrTimeDialog(Context context) {df = new SimpleDateFormat("HH:mm:SS");switch (444) {case DATE:date: new DatePickerDialog(context, listener,cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH)).show();break;case TIME:System.out.println("----------time---------------");new TimePickerDialog(context, timeListen,cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),true).show();break;default:new DatePickerDialog(context, listener, cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH)).show();}}                private DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { //@Overridepublic void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {cal.set(Calendar.YEAR, arg1);cal.set(Calendar.MONTH, arg2);cal.set(Calendar.DAY_OF_MONTH, arg3);updateDate();}};private void updateDate() {edit.setText(df.format(cal.getTime()));}TimePickerDialog.OnTimeSetListener timeListen = new TimePickerDialog.OnTimeSetListener() {// 同DatePickerDialog控件@Overridepublic void onTimeSet(TimePicker view, int hourOfDay, int minute) {cal.set(Calendar.HOUR_OF_DAY, hourOfDay);cal.set(Calendar.MINUTE, minute);cal.set(Calendar.SECOND, cal.get(Calendar.SECOND));updateTimes();}};// 更新页面TextView的方法private void updateTimes() {edit.setText(df.format(cal.getTime()));}    }

说明的:

其中SimpleDateFormatdf = new SimpleDateFormat("HH:mm:SS"); //定义获取时间的样子

switch (444) {case DATE:date: new DatePickerDialog(context, listener,cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH)).show();break;case TIME:System.out.println("----------time---------------");new TimePickerDialog(context, timeListen,cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE),true).show();break;default:new DatePickerDialog(context, listener, cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH)).show();}

定义的对话框中,我们要不要显示时间,你可以更改switch(DATA||TIME)来选择你要的样子。

如果你还是看不懂上面的代码,或者用起来有问题,那么你可以联系:qq 1019990976。

觉得是无图有真相的。

更多相关文章

  1. android自定义View-垂直滚动的TextView
  2. android如何使用自定义JNI接口,以及NDK工具的环境搭建与使用。
  3. listview(1、BaseAdapter)
  4. Android麦克风录音带音量大小动态显示的圆形自定义View
  5. UI控件--自定义SeekBar样式
  6. Android(安卓)Butterknife 8.4.0 使用方法总结
  7. 安卓入门--Spinner控件与二级联动详解
  8. 【Android(安卓)开发】:Android中普通按钮的使用方法
  9. Android(安卓)播放 Gif 图片控件

随机推荐

  1. ffmpeg3.3 编译 android 系统版本 32位
  2. mmap那些事之android property实现之二
  3. Android高级工程师必备知识点--更新中
  4. android定时器实现
  5. Android对象序列化存储
  6. Android 适配 全面屏/刘海屏
  7. android,饼图,扇形图,点击,转动,随手记,可以转
  8. Android中TextView 添加ClickableSpan后
  9. android emulator 命令详解
  10. android 动态添加控件控制属性问题