2014-09-26 97 views
4

我想將DatePickerDialog的值設置爲EditText(sdf.format(myCalendar.getTime()));。我試過這種方式,但它不起作用。如何解決它?如何將DatePickerDialog值設置爲EditText?

下面是我的代碼:

Calendar myCalendar; 
DatePickerDialog.OnDateSetListener date; 

private int pYear; 
private int pMonth; 
private int pDay; 
static final int DATE_DIALOG_ID = 0; 

myCalendar = Calendar.getInstance(); 
pYear = myCalendar.get(Calendar.YEAR); 
pMonth = myCalendar.get(Calendar.MONTH); 
pDay = myCalendar.get(Calendar.DAY_OF_MONTH); 

dobET.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     showDialog(DATE_DIALOG_ID); 
    } 
}); 

public Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DATE_DIALOG_ID: 
     date = new DatePickerDialog.OnDateSetListener() { 
      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
       pYear = year; 
       pMonth = monthOfYear; 
       pDay = dayOfMonth; 

       updateLabel(); 
      } 
     }; 
     DatePickerDialog dialog = new DatePickerDialog(this, date, pYear, pMonth, pDay); 
     dialog.getDatePicker().setMaxDate(new Date().getTime()); 
     return dialog; 
    } 
    return null; 
} 

private void updateLabel() { 
    String myFormat = "MM-dd-yyyy"; // In which you need put here 
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); 

    String myFormat2 = "yyyy-MM-dd"; // In which you need put here 
    SimpleDateFormat sdf2 = new SimpleDateFormat(myFormat2, Locale.US); 
    dateToSave = sdf2.format(myCalendar.getTime()); 
    saveDOBPrefEditor.putString("DOB", dateToSave); 
    saveDOBPrefEditor.commit(); 

    dobET.setText(sdf.format(myCalendar.getTime())); 
} 
+0

您更新'pBlaBla'在'onDateSet()'中,但是你可以在'updateLabel()'中使用'myCalendar'。更新任一個,然後將它們用於'updateLabel()'。 – 2014-09-26 06:15:19

+0

你可以寫代碼我很困惑這個 – Aparana 2014-09-26 06:30:22

+0

@ user3876078試試我的答案,如果它不起作用告訴我我會編輯你的 – Umair 2014-09-26 06:32:03

回答

0

試試這個

​​

希望這有助於....

+0

我曾嘗試過這段代碼,但它沒有更新編輯文本 – Aparana 2014-09-26 06:35:10

+0

我的問題是隻更新datepicker的編輯文本值 – Aparana 2014-09-26 06:35:56

+0

@ user3876078日期選擇器會讓你當前的日期...你想從日期選擇器中獲得什麼價值? – Umair 2014-09-26 06:36:43

1
calendar = Calendar.getInstance(); 
     day = calendar.get(Calendar.DAY_OF_MONTH); 
     month = calendar.get(Calendar.MONTH); 
     year = calendar.get(Calendar.YEAR);  

date.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       date.setCursorVisible(false); 
       datePickerDialog = new DatePickerDialog(CityList.this, 
         new OnDateSetListener() { 

          @Override 
          public void onDateSet(DatePicker view, int year1, 
            int monthOfYear, int dayOfMonth) { 
           // TODO Auto-generated method stub 

           year = year1; 
           month = monthOfYear; 
           day = dayOfMonth; 
           datePickerDialog.updateDate(year, monthOfYear, 
             dayOfMonth); 
           date.setText((month + 1) + "/" + day + "/" 
             + year); 
          } 

         }, year, month, day); 

       // 
       datePickerDialog.setTitle("Select Date"); 
       datePickerDialog.show(); 
      } 

     }); 
+0

只是複製過去這段代碼希望這會起作用 – 2014-09-26 09:58:52

0

您可以通過點擊的DatePicker按鈕使用TextView代替EditText和更改日期

public class ProfileDetailActivity extends ActionBarActivity { 

    ImageButton birthdate_selector; 
    TextView user_birth_date; 
    Context mContext; 
    Calendar calendar; 
    SimpleDateFormat format; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mContext = ProfileDetailActivity.this; 
    setContentView(R.layout.fragment_profile_edit); 
    birthdate_selector = (ImageButton) findViewById(R.id.birthdate_selector); 
    user_birth_date = (TextView) findViewById(R.id.user_birth_date); 

    birthdate_selector.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      DialogFragment newFragment = new DatePickerFragment(); 
      newFragment.show(getSupportFragmentManager(), "datePicker"); 
     } 
    }); 

} 

@SuppressLint({ "ValidFragment", "SimpleDateFormat" }) 
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     int monthInt = 0, year = 0, month = 0, day = 0; 
     String[] separated = intent.getStringExtra("birth_date").split(" "); 
     calendar = Calendar.getInstance(); 
     try { 
       calendar.setTime(new SimpleDateFormat("MMM").parse(separated[1])); 
       monthInt = calendar.get(Calendar.MONTH); 
       year = Integer.parseInt(separated[3]); 
       month = monthInt; 
       day = Integer.parseInt(separated[0]); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      if (Config.isDebugEnabled) { 
        Log.e("Date in set:", "day: " + day + " month : " + month + " year: " + year); 
      } 
      // Create a new instance of DatePickerDialog and return it 
      return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int month, int day) { 
     final Calendar c = Calendar.getInstance(); 
     int yy = c.get(Calendar.YEAR); 
     int mm = c.get(Calendar.MONTH); 
     int dd = c.get(Calendar.DAY_OF_MONTH); 
     format = new SimpleDateFormat("yyyy-MM-dd"); 
     java.util.Date date1 = null; 
     java.util.Date date2 = null; 
     try { 
       date1 = format.parse(year + "-" + (month + 1) + "-" + day); 
       date2 = format.parse(yy + "-" + mm + "-" + dd); 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      int diffInDays = (int) ((date2.getTime() - date1.getTime())/(1000 * 60 * 60 * 24)); 
      int diffInYears = diffInDays/365; 
      if (type.equals("parent")) { 
        if (diffInYears < 15) { 
          user_birth_date.setError("Please enter valid birthdate ."); 
          user_birth_date.setText(null); 
        } else { 
          format = new SimpleDateFormat("d"); 
          calendar = new GregorianCalendar(year, month, day); 
          format = new SimpleDateFormat("d MMM , yyyy"); 
          view_date = format.format(calendar.getTime()); 
          // view_date = year+"-"+(month+1)+"-"+day; 
          user_birth_date.setText(view_date); 
        } 
      } 
      if (type.equals("student")) { 
        if (diffInYears < 3) { 
          user_birth_date.setError("Please enter valid birthdate ."); 
          user_birth_date.setText(null); 
        } else { 
          format = new SimpleDateFormat("d"); 
          calendar = new GregorianCalendar(year, month, day); 
          format = new SimpleDateFormat("d MMM , yyyy"); 
          view_date = format.format(calendar.getTime()); 
          // view_date = year+"-"+(month+1)+"-"+day; 
          user_birth_date.setText(view_date); 
        } 
      } 
     } 
    } 
} 
0

試試這個順序:

以XML

<EditText 
     android:id="@+id/etDate" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="2dp" 
     android:inputType="number" 
     android:onClick="showDatePickerDialog" //on click of this edittext showDatePickerDialog below method will be called 
     android:textSize="12dp" /> 

在活動課

EditText etDate = (EditText) findViewById(R.id.etDate); 

public void showDatePickerDialog(View v) { 
     DialogFragment newFragment = new DatePickerFragment(); 
     newFragment.show(getFragmentManager(), "datePicker"); 
    } 

private class DatePickerFragment extends DialogFragment implements OnDateSetListener 
    { 
     @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); 

      return new DatePickerDialog(getActivity(), this, year, month, day); 
     } 

     public void onDateSet(DatePicker view, int year, int month, int day) 
     { 
      etDate.setText("" + day + "/" + (month + 1) + "/" + year); 
     } 
    } 

和你做:)

相關問題