繁体   English   中英

如何将DatePickerDialog的值传递给其他editText?

[英]How to passed the value of DatePickerDialog to different editText?

我创建了两个不同的editText, 先前的检查日期当前的检查日期 我正在使用fragmentDatePickerDialog来获取日期。 每个editText都有自己的日期。 如何在datePickerDialog中传递日期?

ps我正在使用onFocusChange,以便用户单击editText时弹出对话框。 使用setOnClickListener将要求用户双击以显示datePickerDialog。

FormFragment.java

public class FormFragment extends Fragment {

....

    //TODO : Fixed datepicker & time picker

    //get Date func
    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    //get time func
    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }


    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //get CaseDetailActivity
        final Activity activity = this.getActivity();

        /*
        Use onFocusChangeListener - the dialog automatically popped when clicked on edittext
        */

        //inspection date
        final View editText_date = activity.findViewById(R.id.input_inspecDateNew);
        editText_date.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    //get current text
                    showDatePickerDialog(v);
                }
            }
        });

        //previous inspection date
        View editText_prevDate = activity.findViewById(R.id.input_lastInspecDate);
        editText_prevDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    //get current text
                    showDatePickerDialog(v);
                }
            }
        });
    }
}

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    public static EditText editText;

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

    }

    public void getCurrentDate () {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = df.format(c.getTime());

        editText.setText(formattedDate);
    }

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

        editText.setText(formattedDate);
    }
}

您可以使用setTargetFragmentonActivityResult实现此目的,

你可以这样尝试

FormFragment.java

public class FormFragment extends Fragment {

    private static final String TAG = FormFragment.class.getSimpleName();

    private static final int REQUEST_CODE_INSPECDATENEW = 200;
    private static final int REQUEST_CODE_LASTINSPECDATE = 201;

    private EditText editText_date;
    private EditText editText_prevDate;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank2, container, false);
    }

    //get Date func
    public void showDatePickerDialog(View v, int requestCode) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.setTargetFragment(this, requestCode);
        newFragment.show(getFragmentManager(), "datePicker");
    }

    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //inspection date
        editText_date = view.findViewById(R.id.input_inspecDateNew);
        editText_date.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    //get current text
                    showDatePickerDialog(v, REQUEST_CODE_INSPECDATENEW);
                }
            }
        });

        //previous inspection date
        editText_prevDate = view.findViewById(R.id.input_lastInspecDate);
        editText_prevDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    //get current text
                    showDatePickerDialog(v, REQUEST_CODE_LASTINSPECDATE);
                }
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        Log.d(TAG, "onActivityResult: ");

        if (resultCode == Activity.RESULT_OK) {
            if (data == null) {
                return;
            }

            String dataValue = data.getStringExtra("value");

            if (requestCode == REQUEST_CODE_INSPECDATENEW) {
                editText_date.setText(dataValue);
            } else if (requestCode == REQUEST_CODE_LASTINSPECDATE) {
                editText_prevDate.setText(dataValue);
            }
        }
    }
}

fragment_blank2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
              android:orientation="vertical"
             android:layout_height="match_parent">

    <EditText
        android:id="@+id/input_inspecDateNew"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment"/>

    <EditText
        android:id="@+id/input_lastInspecDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment"/>

</LinearLayout>

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

//    public static EditText editText;

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

    }

    public void getCurrentDate() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = df.format(c.getTime());


//        editText.setText(formattedDate);
    }

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

        Intent intent = new Intent();
        intent.putExtra("value", formattedDate);

        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
//        editText.setText(formattedDate);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM