2017-03-27 133 views
0

嗨我有一个问题是否有无论如何,我可以从包含日期的textView开始datepicker的日期?开始日期字符串日期选择器日期

这里是我的代码:

public void openDatePicker() 
{ 
    // Get Current Date 
    final Calendar c = Calendar.getInstance(); 
    mYear = c.get(Calendar.YEAR); 
    mMonth = c.get(Calendar.MONTH); 
    mDay = c.get(Calendar.DAY_OF_MONTH); 
    final DatePickerDialog datePickerDialog = new DatePickerDialog(rentActivity.this, 
     new DatePickerDialog.OnDateSetListener() { 
      @Override 
      public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) 
      { 
       endDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year); 
      } 
     }, mYear, mMonth, mDay); 

    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis()-1000); 
    datePickerDialog.show(); 
} 

,并包含日期字符串是txtDate

感谢您的帮助提前! :D

+1

你可以采取一个TextView的,并在该展会日期选取器 – curiousMind

+0

的点击@curiousMind先生其实是我想知道就是它。比如说,我有一个包含日期25/1/2016的文本,当我打开日期选择器时,它将从textview中的日期开始:D –

回答

0

请进行以下更改功能,我刚才提到的意见修改的地方已经完成

public void openDatePicker() { 
     // Get Current Date 
     final DatePickerDialog datePickerDialog = new DatePickerDialog(this, 
       new DatePickerDialog.OnDateSetListener() { 

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

         endDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year); 

        } 
       }, mYear, mMonth, mDay); 

     // get the date from TextView 
     String startDate = txtDate.getText().toString(); 
     Calendar calendar = Calendar.getInstance(); 
     Date date = new Date(); 
     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault()); 
     try { 
      date = simpleDateFormat.parse(startDate); // parse the String according to the desired format 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     // set the date as minDate 
     datePickerDialog.getDatePicker().setMinDate(date.getTime()); 
     datePickerDialog.show(); 
    } 
+0

先生它的工作原理,非常感谢你! :D –

+0

很高兴为您提供帮助 –

0

在实例化日历对象后插入它。

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dd/MM/yyyy); 
Date date = simpleDateFormat.parse(textView.getText().toString()); 
c.setTime(date); 

要小心format the SimpleDateFormat以满足您的需求。

+0

谢谢先生,因为你回答我很感激。其实我想过使用datepickerdialog.updateDate();功能,但我想有另一种方式哈哈:D –

相关问题