Set fragment edittext value from dialogfragment
我正在尝试创建searchdate。 这是我在这里做的是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_rijek_search, container, false); EditText start = (EditText) v.findViewById(R.id.startdateval); start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); DatePickerFragment myDialogFragment = new DatePickerFragment(); myDialogFragment.show(fm,"dialog_fragment"); } }); return v; } |
这是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @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); 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()); Fragment fr = new RijekSearch(); FragmentManager fm=getFragmentManager(); FragmentTransaction ft=fm.beginTransaction(); Bundle args = new Bundle(); args.putString("startval", formattedDate); fr.setArguments(args); ft.replace(R.id.content_frame, fr); ft.commit(); } } |
所以。 首先,我打开
我的问题是,在从
您需要将
首先删除此
1 -on文本更改侦听器
2 - FocusChangeListener
3 - 搜索并处理其点击
on Action传递
1 2 3 4 5 | Bundle args = new Bundle(); args.putString("key","value"); DialogFragment newFragment = new YourDialogFragment(); newFragment.setArguments(args); newFragment.show(getSupportFragmentManager(),"TAG"); |
并在
1 2 | Bundle mArgs = getArguments(); String myValue = mArgs.getString("key"); |
创建接口
1 2 3 | interface DateCallback{ void onDateSet(String fDate); } |
在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { private static DataCallback dataCallback; public static DatePickerFragment newInstance(DataCallback dataCallback){ this.dataCallback = dataCallback; return new DatePickerFragment(); } @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); 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()); FragmentManager fm=getFragmentManager(); dataCallback.onDateSet(formattedDate); fm.popBackStack(); } } |
现在在你的RijekSearch中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fm = getFragmentManager(); DatePickerFragment myDialogFragment = DatePickerFragment.newInstance(new DateCallback(){ @Override public void onDateSet(String fDate){ start.setText(fDate); } }); myDialogFragment.show(fm,"dialog_fragment"); } }); |
有两种方法:
1.创建一个公共静态editText(不推荐,内存泄漏)。
2.创建侦听器并在对话框片段中传递实例。