One*_*Two 0 android android-fragments
我已经向MainActivity添加了一个带有日期选择器的对话框片段,它工作正常.我现在想将使用日期选择器选择的日期传递给MainActivity调用的新活动.所以我使用意图在MainActivity和新活动之间进行通信.
但是,我如何才能将从对话框片段中选择的日期传递给MainActivity?
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public static String formattedDate = "com.example.myfirstapp.DATEOB";
@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);
Run Code Online (Sandbox Code Playgroud)
Xav*_*ler 10
在a Fragment和a之间传递数据的最佳实践解决方案Activity是使用侦听器模式.
首先,我们需要定义一个接口.我们添加一个onDateSet()使用Date参数调用的方法.它将用于将Date背面传递给Activity:
public interface DatePickerFragmentListener {
public void onDateSet(Date date);
}
Run Code Online (Sandbox Code Playgroud)
我建议您将此接口嵌套在DatePickerFragment以下内容中:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public interface DatePickerFragmentListener {
public void onDateSet(Date date);
}
...
}
Run Code Online (Sandbox Code Playgroud)
我们需要添加一个成员变量DatePickerFragment来保存对侦听器的引用.我们还需要侦听器的getter和setter方法以及安全调用侦听器的notify方法:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private DatePickerFragmentListener datePickerListener;
public interface DatePickerFragmentListener {
public void onDateSet(Date date);
}
public DatePickerFragmentListener getDatePickerListener() {
return this.datePickerListener;
}
public void setDatePickerListener(DatePickerFragmentListener listener) {
this.datePickerListener = listener;
}
protected void notifyDatePickerListener(Date date) {
if(this.datePickerListener != null) {
this.datePickerListener.onDateSet(date);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
最佳实践是使用静态工厂方法来创建新Fragment实例.除了方便之外,这样做的许多好处之一是你可以定义一个Fragment正确设置的方法- 例如设置监听器,将值传递给Fragment等等 - 并且你不会冒险忘记某些东西或者稍后混合某些东西.在我们的例子中,它需要设置DatePickerFragmentListener:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
public static DatePickerFragment newInstance(DatePickerFragmentListener listener) {
DatePickerFragment fragment = new DatePickerFragment();
fragment.setDatePickerListener(listener);
return fragment;
}
...
}
Run Code Online (Sandbox Code Playgroud)
现在仍然失踪的唯一的事情是,我们需要调用notifyDatePickerListener()中的回调DatePicker中DatePickerFragment:
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
Date date = c.getTime();
// Here we call the listener and pass the date back to it.
notifyDatePickerListener(date);
}
Run Code Online (Sandbox Code Playgroud)
在你Activity我们只需要调用newInstance()我们添加的方法来创建一个新的正确设置DatePickerFragment.我们还需要通过DatePickerFragmentListener进入newInstance().我建议你让你的Activity实现DatePickerFragmentListener界面:
public class MainActivity extends Activity implements DatePickerFragmentListener {
...
@Override
public void onDateSet(Date date) {
// This method will be called with the date from the `DatePicker`.
}
}
Run Code Online (Sandbox Code Playgroud)
您只需要DatePickerFragment使用该newInstance()方法创建一个新的实例,并传入Activity以使其工作:
DatePickerFragment fragment = DatePickerFragment.newInstance(this);
Run Code Online (Sandbox Code Playgroud)
就是这样!以这种方式实现它可能是10行代码,但它是100倍的更好的解决方案.
我测试了一切,它运行得很好.我希望我可以帮助你,如果你有任何其他问题,请随时提出!
| 归档时间: |
|
| 查看次数: |
3648 次 |
| 最近记录: |