我创建了一个sprites.clear(screen,clear_callback)
sprites.update()
sprites.draw(screen)
,我从DateandTimePickerDialogueFragment
拨打了DateandTimePickerDialogueFragment
。
我的问题是,如何将MainActivity
结果值发送到DateandTimePickerDialogueFragment
。
MainActivity:
MainActivity
DateandTimePickerDialogueFragment:
@Override
public void onClick(View v) {
if (v.getId() == R.id.buttonid) {
DateandTimePickerDialogueFragment dateandTimePickerDialogueFragment = new DateandTimePickerDialogueFragment();
dateandTimePickerDialogueFragment.show(getSupportFragmentManager(), "DateandTimePickerDialogueFragment");
}
}
答案 0 :(得分:0)
在DialogFragment
中,您可以为此设置界面
用以下代码替换完整代码
public class DateandTimePickerDialogueFragment extends DialogFragment {
public static interface OnCompleteListener {
public abstract void onComplete(String time);
}
private OnCompleteListener mListener;
// make sure the Activity implemented it
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
this.mListener = (OnCompleteListener) activity;
} catch (final ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return displayDateDialogPicker(getActivity());
}
/**
* @param context
*/
private Dialog displayDateDialogPicker(Context context) {
mListener = (OnCompleteListener)getActivity();
int mYear, mMonth, mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(context,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int mymonth = monthOfYear + 1;
String mmmMonth = "", mmDate = "";
if (mymonth > 0 && mymonth < 10) {
mmmMonth = "0" + mymonth;
} else {
mmmMonth = "" + mymonth;
}
if (dayOfMonth > 0 && dayOfMonth < 10) {
mmDate = "0" + dayOfMonth;
} else {
mmDate = "" + dayOfMonth;
}
//YYYY-MM-DD
String finalDate = year + "-" + (mmmMonth) + "-" + mmDate;
mListener.onComplete(finalDate);
// getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intentData);
}
}, mYear, mMonth, mDay);
dpd.show();
return dpd;
}
}
现在,在您的活动中,您可以implements DateandTimePickerDialogueFragment.OnCompleteListener
并导入界面的方法,在此方法中您可以获取数据
@Override
public void onComplete(String time) {
Log.d("TAG", time.toString());
}
答案 1 :(得分:0)
虽然在DialogFragment上并且在返回dpd之前没有使用包含方法或valueType的接口,但是在该变量接口中插入该dpd并在MainActivity上实现该接口。
答案 2 :(得分:0)
您需要设置一个接口侦听器。
在对话框片段中:
public interface DateandTimeListener {
public void OnDateTimeSelected(String date);
}
private static DateandTimeListener mListener;
//Your activity will call this. Passing its context 'this' as the listener.
public void setDateandTimeListener(DateandTimeListener listener){
mListener = listener;
}
然后在你的onDateSet:
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth){
....
String finalDate = year + "-" + (mmmMonth) + "-" + mmDate;
mListener.OnDateTimeSelected(finalDate);
....
}
在您的MainActivity中:
确保您实现了侦听器。
public class MainActivity extends Activity implements DateandTimePickerDialogueFragment.DateandTimeListener
这将强制您覆盖活动中的OnDateTimeSelected
回调函数。从那里,您将获得从对话框返回的finalDate字符串。
同样在您的活动中,请务必致电setDateandTimeListener(this)
设置听众
希望有所帮助!