2017-08-04 64 views
1

我已經創建了兩個不同的editText,以前的檢查日期當前檢查日期。我正在使用片段DatePickerDialog以獲取日期。每個editText都會有自己的日期。我如何通過datePickerDialog中的日期?我使用onFocusChange如何將DatePickerDialog的值傳遞給不同的editText?

P.S,這樣的對話框彈出時對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); 
    } 
} 

回答

0

您可以使用setTargetFragmentonActivityResult實現這一目標,

你可以試試樣T他

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); 
    } 
} 
+0

謝謝您的幫助。它像一個魅力。但是這個代碼'editText_date = view.findViewById(R.id.input_inspecDateNew);',必須改爲'editText_date =(EditText)view.findViewById(R.id.input_inspecDateNew);' – xxmilcutexx

+0

歡迎你,你可以使用你想要的東西,只是我舉了一個例子,我們該如何處理它。 –

相關問題