标签:
效果图如下。
实现也很简单,使用的是Android的DatePicker控件。AlertDialog实现。
首先是布局文件: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> </LinearLayout>
实现类:DoubleDatePickerDialog.java
package com.example.doubledatepicker; import java.lang.reflect.Field; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; public class DoubleDatePickerDialog extends AlertDialog implements OnClickListener, OnDateChangedListener { private static final String START_YEAR = "start_year"; private static final String END_YEAR = "end_year"; private static final String START_MONTH = "start_month"; private static final String END_MONTH = "end_month"; private static final String START_DAY = "start_day"; private static final String END_DAY = "end_day"; private final DatePicker mDatePicker_start; //开始 private final OnDateSetListener mCallBack; public interface OnDateSetListener { void onDateSet(DatePicker startDatePicker, int startYear, int startMonthOfYear, int startDayOfMonth); } 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); } 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); if (!isDayVisible) { hidDay(mDatePicker_start); } } /** * 隐藏DatePicker中的日期显示 * * @param mDatePicker */ private void hidDay(DatePicker mDatePicker) { Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields(); for (Field datePickerField : datePickerfFields) { if ("mDaySpinner".equals(datePickerField.getName())) { datePickerField.setAccessible(true); Object dayPicker = new Object(); try { dayPicker = datePickerField.get(mDatePicker); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } ((View) dayPicker).setVisibility(View.GONE); } } } 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; } public void updateStartDate(int year, int monthOfYear, int dayOfMonth) { mDatePicker_start.updateDate(year, monthOfYear, dayOfMonth); } public void updateEndDate(int year, int monthOfYear, int 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); int end_year = savedInstanceState.getInt(END_YEAR); int end_month = savedInstanceState.getInt(END_MONTH); int end_day = savedInstanceState.getInt(END_DAY); } }
使用时创建出来对象实现onDateSet方法就可以了。测试类xml文件: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>
测试类,MainActivity.java
package com.example.doubledatepicker; import java.util.Calendar; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; 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) { 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), true).show(); } }); } }
清单文件:有些版本显示的是不同的效果,注意一下sdk版本问题。
标签:
原文地址:http://www.cnblogs.com/yaxiaoke/p/4802359.html