简单的日期选择控件的实现(弹出日历,选择日期)

xiaoxiao2021-02-28  52

项目地址:https://github.com/vikramkakkar/SublimePicker

星星 一千八的  值得拥有

很简单  直接compile  之后 就是 界面 自定义 DialongFragmeng activity代码 三个部分。

首先界面:

<EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="点击选择时间 "/>

然后 自定义DialogFragment:

//官方推荐的对话框碎片 //DialogFragment 和Fragment一样的生命周期 public class SublimePickerFragment extends DialogFragment { SublimePicker mSublimePicker; Callback mCallback; DateFormat mDateFormatter, mTimeFormatter; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mSublimePicker = (SublimePicker) getActivity().getLayoutInflater().inflate(R.layout.sublime_picker, container); Bundle arguments = getArguments(); SublimeOptions options = null; if (arguments != null) { options = arguments.getParcelable("SUBLIME_OPTIONS"); } mSublimePicker.initializePicker(options, mListener); return mSublimePicker; } SublimeListenerAdapter mListener = new SublimeListenerAdapter() { @Override public void onCancelled() { if (mCallback != null) { mCallback.onCancelled(); } dismiss(); } @Override public void onDateTimeRecurrenceSet(SublimePicker sublimeMaterialPicker, SelectedDate selectedDate, int hourOfDay, int minute, SublimeRecurrencePicker.RecurrenceOption recurrenceOption, String recurrenceRule) { if (mCallback != null) { mCallback.onDateTimeRecurrenceSet(selectedDate, hourOfDay, minute, recurrenceOption, recurrenceRule); } dismiss(); } }; //构造函数 // 初始化格式机器 设置时区 public SublimePickerFragment() { mDateFormatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault()); mTimeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault()); mTimeFormatter.setTimeZone(TimeZone.getTimeZone("GMT+0")); } public void setCallback(Callback callback) { mCallback = callback; } // For communicating with the activity public interface Callback { void onCancelled(); void onDateTimeRecurrenceSet(SelectedDate selectedDate, int hourOfDay, int minute, SublimeRecurrencePicker.RecurrenceOption recurrenceOption, String recurrenceRule); } }

最后activity代码:

public class Sampler extends AppCompatActivity { SelectedDate mSelectedDate; //回调函数 SublimePickerFragment.Callback mFragmentCallback = new SublimePickerFragment.Callback() { @Override public void onCancelled() { } @Override public void onDateTimeRecurrenceSet(SelectedDate selectedDate, int hourOfDay, int minute, SublimeRecurrencePicker.RecurrenceOption recurrenceOption, String recurrenceRule) { mSelectedDate = selectedDate; updateInfoView(); } }; private EditText edit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sampler); // Initialize views to display the chosen Date, Time & Recurrence options edit = (EditText)findViewById(R.id.et); edit.setInputType(InputType.TYPE_NULL); //设置点击事件跳转到日历界面。 edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SublimePickerFragment pickerFrag = new SublimePickerFragment(); pickerFrag.setCallback(mFragmentCallback); //将信息传入到options中。 SublimeOptions options = new SublimeOptions(); int displayOptions = 0; displayOptions |= SublimeOptions.ACTIVATE_DATE_PICKER; options.setPickerToShow(SublimeOptions.Picker.DATE_PICKER); options.setDisplayOptions(displayOptions); options.setCanPickDateRange(true); // Valid options Bundle bundle = new Bundle(); bundle.putParcelable("SUBLIME_OPTIONS", options); pickerFrag.setArguments(bundle); pickerFrag.setStyle(DialogFragment.STYLE_NO_TITLE, 0); // pickerFrag.setStyle(DialogFragment.STYLE_NO_TITLE, 0); pickerFrag.show(getSupportFragmentManager(), "SUBLIME_PICKER"); } }); } // Show date, time & recurrence options that have been selected //显示选中的时间。 private void updateInfoView() { if (mSelectedDate != null) { edit.setText(applyBoldStyle("") .append(String.valueOf(mSelectedDate.getStartDate().get(Calendar.YEAR))) .append("-") .append(mSelectedDate.getStartDate().get(Calendar.MONTH) < 9 ? "0" + String.valueOf(mSelectedDate.getStartDate().get(Calendar.MONTH) + 1):String.valueOf(mSelectedDate.getStartDate().get(Calendar.MONTH) + 1)) .append("-") .append(mSelectedDate.getStartDate().get(Calendar.DAY_OF_MONTH) < 10 ? "0" + String.valueOf(mSelectedDate.getStartDate().get(Calendar.DAY_OF_MONTH)):String.valueOf(mSelectedDate.getStartDate().get(Calendar.DAY_OF_MONTH))) ); } } // Applies a StyleSpan to the supplied text private SpannableStringBuilder applyBoldStyle(String text) { SpannableStringBuilder ss = new SpannableStringBuilder(text); ss.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); return ss; } }

转载请注明原文地址: https://www.6miu.com/read-1100270.html

最新回复(0)