我有一个DialogFragment打开另一个实现DatePicker的DialogFragment,我想将DatePicker中的日期传递回前一个DialogFragment并将其显示在EditText上。我找到了将日期传递回主要活动的方法,但我找不到将其传递回上一个DialogFragment的方法。 请帮助。
我使用此处的示例添加DatePicker: https://neurobin.org/docs/android/android-date-picker-example/
这是我的代码中的类: MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new MyDialogFragment();
newFragment.show(getSupportFragmentManager(), "DialogFragment");
}
});
}
MyDialogFragment
public class MyDialogFragment extends DialogFragment {
EditText edTo;
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.popup, container, false);
getDialog().setTitle("Set Date Range");
edTo = (EditText) rootView.findViewById(R.id.etDateTo);
edTo.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
}
return false;
}
});
return rootView;
}
}
DatePickerFragment 公共类DatePickerFragment extends DialogFragment实现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 monthOfYear, int dayOfMonth) {
// Do something with the date chosen by the user
EditText etDateTo= (EditText) getActivity().findViewById(R.id.tv);
etDateTo.setText("Year: "+year+" Month: "+monthOfYear+" Day: "+dayOfMonth);
}
}
这是打开DatePicker的MyDialogFragment enter image description here
答案 0 :(得分:1)
EventBus为我解决了这个问题。我使用了这个链接的例子incasae任何人面临同样的问题。 http://www.cardinalsolutions.com/blog/2015/02/event-bus-on-android 谢谢,
答案 1 :(得分:0)
为了从片段接收事件回调,托管它的活动必须实现接口。
foreach(var paragraph in myText)
{
var span = new HtmlGenericControl("span");
span.InnerHtml = paragraph;
myDiv.Controls.Add(span);
}