I have created two different editText, previous Inspection Date and current inspection date . I'm using fragment and DatePickerDialog to get the dates. Each editText will have their own dates. How do I passed the dates in datePickerDialog?
Ps 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);
}
}
You can use setTargetFragment
and onActivityResult
to achieve this,
You can try like this
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.