设置datepickerdialog日期从的EditText [英] Set date of datepickerdialog from EditText

查看:254
本文介绍了设置datepickerdialog日期从的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常惊讶,我甚至不能1小时后搜索找到这个净!

I am very much surprised that I couldn't find this in net even after 1 hour searching!

我有一个日期选取器对话框由原来的拾荒者例子稍微修改(感谢Raghunandan )。

I have a date picker dialog slightly modified from the original Pickers example (thanks to Raghunandan for this).

但我不希望设置默认的日期作为当前的日期,而是应该选择从EditText上的日期,并设置日期datepickerdialog。任何人都可以帮我解决这个?我现在的code表示如下。

But I do not want to set the default date as current date, rather it should pick the date from EditText and set that date to datepickerdialog. Can anybody please help me solving this? My current code stands as below.

EDITED 我需要知道我怎么能访问的EditText从DatePickerFragment类内部购置日期的值。

EDITED I need to know how I can access the value of EditText datepurchased from inside DatePickerFragment class.

主类:

public class EditEntry extends Activity  implements DatePickerFragment.TheListener{
    EditText edit_datepurchased;
public void showdate(View v) {
        edit_datepurchased = (EditText) findViewById(v.getId());
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }
public void returnDate(String date) {
        edit_datepurchased.setText(date);
    }
}

DatePickerFragment CLASS:

DatePickerFragment CLASS:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
    TheListener listener;

    public interface TheListener{
        public void returnDate(String date);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        final Calendar c=Calendar.getInstance();
        int year=c.get(Calendar.YEAR);
        int month=c.get(Calendar.MONTH);
        int day=c.get(Calendar.DAY_OF_MONTH);
        listener = (TheListener) getActivity(); 
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day){
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(c.getTime());
        if (listener != null) 
        {
          listener.returnDate(formattedDate); 

        }
          //listener.returnDate(year+"-"+(month+1)+"-"+day);
    }
}

n.b:我有一个问题<一href=\"http://stackoverflow.com/questions/19891242/copy-selected-date-from-datepickerdialog-to-edittext-box-in-main-page\">here 之前相同的code,但我不知道如何在旧线增加的问题。

n.b.: I have a question here on same code before, but I don't know how to add question in old thread.

推荐答案

使用的SimpleDateFormat 你可以转换的日期中的EditText以毫秒为单位。

Using SimpleDateFormat you can convert the date in the edittext to milliseconds.

例如:

DateFormat df = new SimpleDateFormat("MM/dd/yyyy",Locale.US);

您需要根据您是如何显示它调整格式,但在那之后只使用从日期格式对象parse方法

you will need to adjust the format based on how you are displaying it but after that just use the parse method from the DateFormat object

Date d = df.parse(dateString);

然后创建一个日历对象,并设置日期

Calendar calendar = Calendar.getInstance();
calendar.setDate(d);

编辑:

发送从文本的的EditText 您需要发送的字符串中包当您创建dialogfragment

to send the text from the EditText you need to send the string in the bundle when you create the dialogfragment

例如:

DialogFragment newFragment = new DatePickerFragment();
Bundle bundle = new Bundle();
bundle.putString("dateAsText",edit_datepurchased.getText().toString());
newFragment.setArguments(bundle); 
newFragment.show(getFragmentManager(), "datePicker");

然后在dialogfragment使用 getArguments()来得到束

Bundle bundle = getArguments();
String date = bundle.getString("dateAsText");

这篇关于设置datepickerdialog日期从的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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