2017-04-14 47 views
0

我是一個絕對的初學者,所以我仍然在學習所有的概念。我試圖用碎片創建一個2日期選擇器的活動。 1表示配置單元設置日期,1表示女王的生日日期 - 兩者都在1個活動中,並且都在單獨的EditText框中填充。有沒有人可以幫助我? Br Iceni養蜂人。Android Date Picker Fragment - 2在同一活動中

+0

的可能的複製[如何設置2個不同的EditText 2 timepicker?](http://stackoverflow.com/questions/41998592/how-to-set工作-2-timepicker-in-2-different-edittext) –

回答

0

嘗試更新的答案對我來說

public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener { 


    public static final String LOGTAG = "APIARYMANAGER"; 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    private static String DATEPICKER_TAG = ""; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 


    public void datePickerHiveSetupDate(View view) { 

     DatePickerFragmentSetupDate fragment = new DatePickerFragmentSetupDate(); 
     fragment.show(getSupportFragmentManager(), "date1"); 
    } 

    public void setHiveQueenBirthdayDate(View view) { 

     DatePickerFragmentQueenBDayDate fragment = new DatePickerFragmentQueenBDayDate(); 

     fragment.show(getSupportFragmentManager(), "date2"); 
    } 


    @Override 
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { 

     Calendar cal = new GregorianCalendar(year, month, dayOfMonth); 

     if (DATEPICKER_TAG.trim().equals("SETUP_DATE")) 
      setHiveSetupDate(cal); 
     else 
      setHiveQBDayDate(cal); 


    } 

    public void setHiveSetupDate(final Calendar calendar) { 

     final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK); 
     ((TextView) findViewById(R.id.editTextHiveSetupDate)).setText(dateFormat.format(calendar.getTime())); 
    } 

    public void setHiveQBDayDate(final Calendar calendar) { 

     final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.UK); 
     ((TextView) findViewById(R.id.editTextHiveQueenBirthday)).setText(dateFormat.format(calendar.getTime())); 
    } 


    public static class DatePickerFragmentSetupDate extends DialogFragment { 
     @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); 
      DATEPICKER_TAG = "SETUP_DATE"; 
      return new DatePickerDialog(getActivity(), 
        (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day); 
     } 


    } 


    public static class DatePickerFragmentQueenBDayDate extends DialogFragment { 
     @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); 
      DATEPICKER_TAG = "SETUP_QUEEN_DATE"; 
      return new DatePickerDialog(getActivity(), 
        (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day); 
     } 

    } 


} 
+0

感謝您的快速回復Sachin。我試圖根據你的代碼實現2日期選擇器。我在onDateSet有問題。我似乎只能有一個方法,所以兩個日期選擇器更新一個EditText框。 – IceniBeekeeper

+0

我該如何發佈我的代碼? – IceniBeekeeper

+0

和你想要更新兩個不同的edittext或一個edittext?請解釋一下。 – Sach