关于android:从dialogfragment设置片段edittext值

Set fragment edittext value from dialogfragment

我正在尝试创建searchdate。 这是我在这里做的是RijekSearch.java

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;
}

这是DatePickerFragment

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();

    }
}

所以。 首先,我打开RijekSearch这是一个片段。 然后在我点击start(EditText)后,它将显示dialogfragment(DatePickerFragment)。 显示没有问题,没有错误。

我的问题是,在从DatePickerFragment中选择日期后,如何设置start值?


您需要将EditText数据一个Fragment传递给DialogFragment

首先删除此start.setOnClickListener(),您可以使用这些

1 -on文本更改侦听器

2 - FocusChangeListener

3 - 搜索并处理其点击

on Action传递EditText数据

1
2
3
4
5
Bundle args = new Bundle();
args.putString("key","value");
DialogFragment newFragment = new YourDialogFragment();  
newFragment.setArguments(args);
newFragment.show(getSupportFragmentManager(),"TAG");

并在DialogFragment中获取值

1
2
Bundle mArgs = getArguments();
String myValue = mArgs.getString("key");


创建接口DateCallback

1
2
3
interface DateCallback{
  void onDateSet(String fDate);
}

DialogFragment params中获取此信息

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.创建侦听器并在对话框片段中传递实例。