如何将 DatePickerDialog 的值传递给不同的 editText?
How to passed the value of DatePickerDialog to different editText?
我创建了两个不同的 editText,上一个检查日期和当前检查日期。我正在使用 fragment 和 DatePickerDialog 来获取日期。每个 editText 都有自己的日期。如何在 datePickerDialog 中传递日期?
P.s I'm using onFocusChange so that the dialog popup when user click on editText. Using setOnClickListener will require user to double click to show the 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);
}
}
您可以使用 setTargetFragment
和 onActivityResult
来实现,
你可以这样试试
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);
}
}
我创建了两个不同的 editText,上一个检查日期和当前检查日期。我正在使用 fragment 和 DatePickerDialog 来获取日期。每个 editText 都有自己的日期。如何在 datePickerDialog 中传递日期?
P.s I'm using onFocusChange so that the dialog popup when user click on editText. Using setOnClickListener will require user to double click to show the 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);
}
}
您可以使用 setTargetFragment
和 onActivityResult
来实现,
你可以这样试试
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);
}
}