如何获取接口包提供previous日期DatePickerDialog? [英] How do I get Interface Bundle to deliver previous date to DatePickerDialog?

查看:240
本文介绍了如何获取接口包提供previous日期DatePickerDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段打开DatePickerDialog。在第一次打开时,对话框的默认日期显示当天的日期。然后,用户选择的日期。当被驳回的对话框中,我设置与用户选择的日期一个EditText行。

打开的对话​​框中的下一次(因为用户希望编辑previously选定的日期),我想告诉pviously选定日期作为默认日期,而不是​​今天的日期用户的$ P $。我已经设置了从片段的接口与一个Bundle活动。此外,也不确定碎片的OnCreateDialog的年我的空虚测试工作正常。请建议。

部分DatePickerFragment文件:

 公共类DatePickerFragment扩展DialogFragment实现DatePickerDialog.OnDateSetListener {私人诠释currentyear;
私人诠释currentmonth;
私人诠释CURRENTDAY;公共DatePickerFragment(){
}公共接口OnDateEnteredListener {
     无效OnDateEntered(INT年,月整型,诠释天);
}
OnDateEnteredListener mListener;
//设置日历对象,将捕获当前日期为DatePickerDialog使用。
日历CAL = Calendar.getInstance();@覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){    最终捆绑datebundle = this.getArguments();
    currentyear = datebundle.get(Calendar.YEAR);
    currentmonth = datebundle.get(的Calendar.MONTH);
    CURRENTDAY = datebundle.get(Calendar.DAY_OF_MONTH);如果(currentyear!= 0){
        DatePickerDialog dateDialog =新DatePickerDialog(this.getActivity(),这一点,currentyear,currentmonth,CURRENTDAY);
        dateDialog.getDatePicker()setCalendarViewShown(真)。
        dateDialog.getDatePicker()setSpinnersShown(假)。
        dateDialog.setTitle(选择到期日);
        返回dateDialog;
    }
    其他{
        //创建三个变量来捕获当前日期。
        currentyear = cal.get(Calendar.YEAR);
        currentmonth = cal.get(的Calendar.MONTH);
        CURRENTDAY = cal.get(Calendar.DAY_OF_MONTH);
        //创建DatePickerDialog它是含有一个datepicker一个简单的对话。使用当前日期
        //作为的DatePicker的默认日期。 DatePickerDialog的新实例被创建合格
        // 5参数/参数构造函数并将其返回。
        DatePickerDialog dateDialog =新DatePickerDialog(this.getActivity(),这一点,currentyear,currentmonth,CURRENTDAY);
        dateDialog.getDatePicker()setCalendarViewShown(真)。
        dateDialog.getDatePicker()setSpinnersShown(假)。
        dateDialog.setTitle(选择到期日);
        返回dateDialog;
    }
}
公共无效onDateSet(查看的DatePicker,年整型,诠释月,日整型){
     mListener.OnDateEntered(年,月,日);
...@覆盖
公共无效onAttach(活动活动){
    super.onAttach(活动);
}

部分活动文件:

 公共类活动扩展AppCompatActivity实现DatePickerFragment.OnDateEnteredListener {  INT currentyear;
  INT currentmonth;
  INT CURRENTDAY;  @覆盖
  保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.input);    fListenerEditText =(ListenerEditText)findViewById(R.id.FEditText);    fListenerEditText.setOnFocusChangeListener(新OnFocusChangeListener(){
        @覆盖
        公共无效onFocusChange(视图V,布尔hasFocus){
        如果(hasFocus&放大器;及(fListenerEditText.getText()长度()== 0)。){
        DialogFragment newFragment =新DatePickerFragment();
        捆绑datebundle =新包();
              datebundle.putInt(年,currentyear);
              datebundle.putInt(月,currentmonth);
              datebundle.putInt(天,CURRENTDAY);
              newFragment.setArguments(datebundle);
              newFragment.show(getSupportFragmentManager(),日期选择器);
           }
        }
    });  @覆盖
  公共无效onDateEntered(INT年,月整型,诠释天){
    吐司面包= Toast.makeText(getApplicationContext(),测试,Toast.LENGTH_LONG);
    toast.show();
    }
}


解决方案

  1. 如果没有找到previous日期,显示用今天的日期沿着对话框 cal.get()


  2. 用户选择日期,将的EditText 来选择日期。


  3. 如果发现previous日期,解析来自的EditText 文本日期格式,用它设置为当前日期 CAL。集(),并使用显示对话框 cal.get()


您可以检查是否的EditText 为空使用 .matches()方法。

例如,

 如果(txtDate.getText()。的toString()。匹配())
         // txtDate是空的

I have a DatePickerDialog that opens in a fragment. On first open, the dialog's default date displays today's date. The user then selects a date. When the dialog is dismissed, I set an EditText line with the user selected date.

The next time the dialog opens (because the user wants to edit their previously selected date), I want to show the user's previously selected date as the default date, not today's date. I've set up an interface from the fragment to the activity with a Bundle. Also, not sure my emptiness test on "year" in Fragment's OnCreateDialog is working properly. Please advise.

partial DatePickerFragment file:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

private int currentyear;
private int currentmonth;
private int currentday;

public DatePickerFragment() {
}

public interface OnDateEnteredListener {
     void OnDateEntered(int year, int month, int day);
}
OnDateEnteredListener mListener;


// Set up a Calendar object that will capture the current date for the DatePickerDialog to use.
Calendar cal = Calendar.getInstance();

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    final Bundle datebundle = this.getArguments();
    currentyear = datebundle.get(Calendar.YEAR);
    currentmonth = datebundle.get(Calendar.MONTH);
    currentday = datebundle.get(Calendar.DAY_OF_MONTH);

if(currentyear != 0){
        DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear, currentmonth, currentday);
        dateDialog.getDatePicker().setCalendarViewShown(true);
        dateDialog.getDatePicker().setSpinnersShown(false);
        dateDialog.setTitle("Select a Due Date");
        return dateDialog;
    }
    else {
        // Create three variables to capture the current date.
        currentyear = cal.get(Calendar.YEAR);
        currentmonth = cal.get(Calendar.MONTH);
        currentday = cal.get(Calendar.DAY_OF_MONTH);
        // Create a DatePickerDialog which is a simple dialog containing a DatePicker.  Use the current date
        // as the default date in the DatePicker.  The new instance of DatePickerDialog is created by passing
        // 5 parameters/arguments to the constructor and returning it.
        DatePickerDialog dateDialog = new DatePickerDialog(this.getActivity(), this, currentyear, currentmonth, currentday);
        dateDialog.getDatePicker().setCalendarViewShown(true);
        dateDialog.getDatePicker().setSpinnersShown(false);
        dateDialog.setTitle("Select a Due Date");
        return dateDialog;
    }
}


public void onDateSet (DatePicker view,int year, int month, int day) {
     mListener.OnDateEntered(year,month,day);
...

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

partial Activity file:

public class Activity extends AppCompatActivity implements DatePickerFragment.OnDateEnteredListener {

  int currentyear;
  int currentmonth;
  int currentday;

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

    fListenerEditText = (ListenerEditText) findViewById(R.id.FEditText);

    fListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus && (fListenerEditText.getText().length() == 0)) {
        DialogFragment newFragment = new DatePickerFragment();
        Bundle datebundle = new Bundle();
              datebundle.putInt("YEAR", currentyear);
              datebundle.putInt("MONTH", currentmonth);
              datebundle.putInt("DAY", currentday);
              newFragment.setArguments(datebundle);
              newFragment.show(getSupportFragmentManager(), "datePicker");
           }
        }
    });

  @Override
  public void onDateEntered(int year, int month, int day) {
    Toast toast = Toast.makeText(getApplicationContext(), "Test", Toast.LENGTH_LONG);
    toast.show();
    }
}

解决方案

  1. If previous date not found, show the dialog along with today's date using cal.get()

  2. User select the date,set the EditText to the date selected.

  3. If previous date found, parse the text from EditText to date format, set it to current date using cal.set(), and display the dialog using cal.get()

You can check whether the EditText is empty using .matches() method.

For example,

if(txtDate.getText().toString().matches(""))
         // txtDate was empty

这篇关于如何获取接口包提供previous日期DatePickerDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆