`
龙哥IT
  • 浏览: 237455 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

获取日期弹出框

 
阅读更多
DoubleDatePickerDialog.java
public class DoubleDatePickerDialog extends AlertDialog implements
		OnClickListener, OnDateChangedListener {

	private static final String START_YEAR = "start_year";
	private static final String START_MONTH = "start_month";
	private static final String START_DAY = "start_day";

	private final DatePicker mDatePicker_start;
	// private final DatePicker mDatePicker_end;
	private final OnDateSetListener mCallBack;

	/**
	 * The callback used to indicate the user is done filling in the date.
	 */
	public interface OnDateSetListener {

		/**
		 * @param view
		 *            The view associated with this listener.
		 * @param year
		 *            The year that was set.
		 * @param monthOfYear
		 *            The month that was set (0-11) for compatibility with
		 *            {@link java.util.Calendar}.
		 * @param dayOfMonth
		 *            The day of the month that was set.
		 */
		void onDateSet(DatePicker startDatePicker, int startYear,
				int startMonthOfYear, int startDayOfMonth);
	}

	/**
	 * @param context
	 *            The context the dialog is to run in.
	 * @param callBack
	 *            How the parent is notified that the date is set.
	 * @param year
	 *            The initial year of the dialog.
	 * @param monthOfYear
	 *            The initial month of the dialog.
	 * @param dayOfMonth
	 *            The initial day of the dialog.
	 */
	public DoubleDatePickerDialog(Context context, OnDateSetListener callBack,
			int year, int monthOfYear, int dayOfMonth) {
		this(context, 0, callBack, year, monthOfYear, dayOfMonth);
	}

	public DoubleDatePickerDialog(Context context, int theme,
			OnDateSetListener callBack, int year, int monthOfYear,
			int dayOfMonth) {
		this(context, 0, callBack, year, monthOfYear, dayOfMonth, true);
	}

	/**
	 * @param context
	 *            The context the dialog is to run in.
	 * @param theme
	 *            the theme to apply to this dialog
	 * @param callBack
	 *            How the parent is notified that the date is set.
	 * @param year
	 *            The initial year of the dialog.
	 * @param monthOfYear
	 *            The initial month of the dialog.
	 * @param dayOfMonth
	 *            The initial day of the dialog.
	 */
	public DoubleDatePickerDialog(Context context, int theme,
			OnDateSetListener callBack, int year, int monthOfYear,
			int dayOfMonth, boolean isDayVisible) {
		super(context, theme);

		mCallBack = callBack;

		Context themeContext = getContext();
		setButton(BUTTON_POSITIVE, "确 定", this);
		setButton(BUTTON_NEGATIVE, "取 消", this);
		setIcon(0);

		LayoutInflater inflater = (LayoutInflater) themeContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.date_picker_dialog, null);
		setView(view);
		mDatePicker_start = (DatePicker) view
				.findViewById(R.id.datePickerStart);
		mDatePicker_start.init(year, monthOfYear, dayOfMonth, this);
	}

	public void onClick(DialogInterface dialog, int which) {
		// 如果是“取 消”按钮,则返回,如果是“确 定”按钮,则往下执行
		if (which == BUTTON_POSITIVE)
			tryNotifyDateSet();
	}

	@Override
	public void onDateChanged(DatePicker view, int year, int month, int day) {
		if (view.getId() == R.id.datePickerStart)
			mDatePicker_start.init(year, month, day, this);
	}

	/**
	 * 获得开始日期的DatePicker
	 * 
	 * @return The calendar view.
	 */
	public DatePicker getDatePickerStart() {
		return mDatePicker_start;
	}

	/**
	 * Sets the start date.
	 * 
	 * @param year
	 *            The date year.
	 * @param monthOfYear
	 *            The date month.
	 * @param dayOfMonth
	 *            The date day of month.
	 */
	public void updateStartDate(int year, int monthOfYear, int dayOfMonth) {
		mDatePicker_start.updateDate(year, monthOfYear, dayOfMonth);
	}

	private void tryNotifyDateSet() {
		if (mCallBack != null) {
			mDatePicker_start.clearFocus();
			mCallBack.onDateSet(mDatePicker_start, mDatePicker_start.getYear(),
					mDatePicker_start.getMonth(),
					mDatePicker_start.getDayOfMonth());
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public Bundle onSaveInstanceState() {
		Bundle state = super.onSaveInstanceState();
		state.putInt(START_YEAR, mDatePicker_start.getYear());
		state.putInt(START_MONTH, mDatePicker_start.getMonth());
		state.putInt(START_DAY, mDatePicker_start.getDayOfMonth());
		return state;
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		int start_year = savedInstanceState.getInt(START_YEAR);
		int start_month = savedInstanceState.getInt(START_MONTH);
		int start_day = savedInstanceState.getInt(START_DAY);
		mDatePicker_start.init(start_year, start_month, start_day, this);
	}
}

 

 

public class MainActivity extends Activity {

	Button btn;
	TextView et;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btn = (Button) findViewById(R.id.dateBtn);
		et = (TextView) findViewById(R.id.et);

		btn.setOnClickListener(new View.OnClickListener() {
			Calendar c = Calendar.getInstance();

			@Override
			public void onClick(View v) {
				// 最后一个false表示不显示日期,如果要显示日期,最后参数可以是true或者不用输入
				new DoubleDatePickerDialog(MainActivity.this, 0, new DoubleDatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear,
							int startDayOfMonth) {
						String textString = String.format("开始时间:%d-%d-%d\n", startYear,
								startMonthOfYear + 1, startDayOfMonth);
						et.setText(textString);
					}
				}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), false).show();
			}
		});
	}
}

 

 

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cursorVisible="false"
        android:editable="false" />

    <Button
        android:id="@+id/dateBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="日期对话框" />

</LinearLayout>

 

date_picker_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:paddingTop="10dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="5dip" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始日期" />

        <DatePicker
            android:id="@+id/datePickerStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:calendarViewShown="false" />
    </LinearLayout>

    <!-- <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/fenge" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="5dip" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="结束日期" />

        <DatePicker
            android:id="@+id/datePickerEnd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:calendarViewShown="false" />
    </LinearLayout>
 -->
</LinearLayout>

  

分享到:
评论

相关推荐

    ymprompt消息提示组件4.0【js插件弹出框美化版】

    fixPosition:设定是否弹出框随滚动条一起浮动,保持在屏幕的固定位置,默认为true dragOut:设定是否允许拖出屏幕范围,默认为false。 autoClose:设定用户点击窗口中按钮后自动关闭窗口,默认为true(设定为...

    jquery移动端底部弹出时间选择框.rar

    H5小程序难免会用到从底部弹出时间选择框,在这里使用了jquery-weui实现了,店铺选择营业时间的功能。已经做好了时间格式和开始时间与结束时间的对比。注意使用的时候可以任意改动样式,但是jquery-weui自带的样式不...

    Extjs5 日期时间公共组件

    样式为横向的日期、时间,非日期弹出框下方选择时间。 注意事项:1、只有日期、年、月、日全部选择后,才能从name中获取值,日期格式:YYYY-MM-dd HH:mm:ss。如果没有全部选择, 则获取空值。 待优化:1、因为使用...

    安卓开发框架工具类相关-里面包含了MD5创建数据库弹出对话框获取日期以及图片缩放等工具类的使用.rar

    里面包含了MD5,创建数据库,弹出对话框,获取日期以及图片缩放等工具类的使用.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    Extjs5 日期时间

    样式显示为横向的日期、时间,非日期弹出框下方选择日期。 调用代码:Ext.create('erp.ux.form.field.DateTimer', { name: 'userBirth'//接收和传递到后台的name }) 注意事项:1、只有日期、年、月、日全部选择后,...

    在文本框中自动显示系统当前时间 年-月-日

    在文本框中自动显示系统当前时间,不用输入,直接获取,方便

    javascript弹出式日期

    直接运用在text框中,可以获取两种日期,一种带时间的,另种是光日期的。。

    Android 日期控件只显示年月

    // 日期弹出框 int SDKVersion = Android_DateActivity.this.getSDKVersionNumber();// 获取系统版本 System.out.println("SDKVersion = " + SDKVersion); DatePicker dp = findDatePicker((ViewGroup) ...

    input框中自动展示当前日期yyyy/mm/dd的实现方法

    下面小编就为大家带来一篇input框中自动展示当前日期yyyy/mm/dd的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    前端html5框架ZUI1.2版

    此版本增加了很多新特性,同时...26、弹出框增加新的选项能够制定JS生成DOM的id属性,便于自定义样式 27、大幅优化代码,修复一些在Javascript代码中的错误,完善关键代码注释,增强部分代码文件与requierejs的兼容性

    Excel VBA实用技巧大全 附书源码

    04147设置单元格区域的除对角框线以外的全部边框 04148删除单元格区域的全部边框 04149设置单元格的颜色和背景 04150设置单元格的格式 04151设置单元格的对齐方式 04152缩小单元格内容以全部显示 04153设定、删除...

    ssm框架酒吧系统完整导入可运行带sql

    首先,它会弹出一个dialog弹框,里面有一个文本框,里面填写的是寄存单号,可以根据寄存单号进行调价查询,之后,通过将查到的数据存放到session中,然后又会弹出一个dialog弹框 里面回显的寄存产品的信息,其中 他...

    《程序天下:JavaScript实例自学手册》光盘源码

    20.18 弹出框式邮件发送 20.19 把网站作为用户的Active桌面 20.20 判断是否安装了flash插件 第21章 流行技术:DOM和userData的应用技巧 21.1 使用userData保存文本内容 21.2 使用userData保存select标记 21.3 使用...

    程序天下:JavaScript实例自学手册

    20.18 弹出框式邮件发送 20.19 把网站作为用户的Active桌面 20.20 判断是否安装了flash插件 第21章 流行技术:DOM和userData的应用技巧 21.1 使用userData保存文本内容 21.2 使用userData保存select标记 21.3 使用...

    JavaScript网页特效范例宝典源码

    1.1 弹出窗口控制 2 实例001 打开新窗口显示广告信息 2 实例002 定时打开窗口 4 实例003 通过按钮创建窗口 5 实例004 自动关闭的广告窗口 6 实例005 控制弹出窗口居中显示 7 实例006 弹出的窗口之Cookie控制 9 实例...

    delphi 开发经验技巧宝典源码

    0223 怎样弹出ConnectionString设置页 148 0224 利用ADO获取DELETE后所影响的记录数 148 7.3 业务实现数据处理技术 149 0225 随机产生中奖号码 149 0226 使用快捷键保存数据 150 0227 密码只允许输入8位...

    android 开发常用工具类集合

    SheetDialog 底部弹出框 LogcatHelper.getInstance(this).start(filePath); 开启保存日志 LogcatHelper.getInstance(this).stop(); 停止保存日志 2018.9.18 添加检查工具类 图片工具类 屏幕工具类 存储工具...

    delphi 开发经验技巧宝典源码06

    0223 怎样弹出ConnectionString设置页 148 0224 利用ADO获取DELETE后所影响的记录数 148 7.3 业务实现数据处理技术 149 0225 随机产生中奖号码 149 0226 使用快捷键保存数据 150 0227 密码只允许输入8位...

    精通JS脚本之ExtJS框架.part2.rar

    10.5 从节点弹出对话框 10.6 节点提示信息 10.7 为节点设置超链接 10.8 树形的拖放 10.8.1 节点拖放的3种形式 10.8.2 叶子不能append 10.8.3 判断拖放的目标 10.8.4 树之间的拖放 10.9 树形过滤器TreeFilter...

Global site tag (gtag.js) - Google Analytics