如何从我的DatePickerFragment转移格式化的datestring?

继官方文档http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker 。

我只用了相同的代码,并在onDateSet方法中只添加了结果格式:

 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker 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); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } @Override 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()); // How to get the string from here to the caller? } } 

为了testing,调用者活动只是显示TextView并在用户触摸widget时调用select器:

 public class OrderHeadEditActivity extends Activity { private TextView mDTDelivery; ... @Override protected void onCreate(Bundle bundle) { ... mDTDelivery = (TextView) findViewById(R.id.order_head_view_dt_delivery); ... mDTDelivery.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DialogFragment picker = new DatePickerFragment(); picker.show(getFragmentManager(), "datePicker"); } }); } ... } 

显示dateselect器,并在将结果格式化为string的地方设置断点后,我可以看到它的工作原理。

但是,我不知道如何将string值传递回mtDTDelivery小部件?

Related of "如何从我的DatePickerFragment转移格式化的datestring?"

正如tyczj指出的,你可以使用接口方法,或者你可以使datapicker类成为你的activity类的一个内部类,在这种情况下,它应该是静态的,而你需要一个弱types的外部类

使用界面。

DatePickerFragment定义一个接口。 在你的活动类中实现接口并将date设置为textview。

 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { TheListener listener; public interface TheListener{ public void returnDate(String date); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker 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(); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } @Override 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); } } } 

主要活动

 public class MainActivity extends Activity implements DatePickerFragment.TheListener{ Button b; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b= (Button) findViewById(R.id.button1); tv= (TextView) findViewById(R.id.textView1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DialogFragment picker = new DatePickerFragment(); picker.show(getFragmentManager(), "datePicker"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void returnDate(String date) { // TODO Auto-generated method stub tv.setText(date); } } 

第二种方法不确定这是否是最好的方法。 我会推荐上面的接口方法。

 public class MainActivity extends Activity {//implements DatePickerFragment.TheListener{ Button b; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b= (Button) findViewById(R.id.button1); tv= (TextView) findViewById(R.id.textView1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DialogFragment picker = new DatePickerFragment(); picker.show(getFragmentManager(), "datePicker"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { private WeakReference<MainActivity> mActivity; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker 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); mActivity = new WeakReference<MainActivity>((MainActivity) getActivity()); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } @Override 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()); MainActivity target = mActivity.get(); if (target != null) target.tv.setText(formattedDate); } } } 

正确的方法是创build一个callback传递string的活动。

看看与一个活动沟通