我想将DatePickerDialog
的值设置为EditText
(sdf.format(myCalendar.getTime()));
。我试过这种方式,但它不起作用。如何解决?
以下是我的代码:
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 :(得分: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();
}
});
答案 1 :(得分:0)
您可以使用TextView
代替EditText
,点击DatePicker按钮更改日期
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);
}
}
}
}
}
答案 2 :(得分:0)
试试这个
DatePickerDialog dialog = null;
Calendar myCalendar = Calendar.getInstance();
dobET.setKeyListener(null);
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
dobET.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(dobET.getWindowToken(), 0);
if(!dialog.isShowing()) {
dialog.show();
}
Log.d(TAG, "Click Listener fired");
}
});
dobET.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean hasFocus) {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(
dobET.getWindowToken(), 0);
if(hasFocus && !(dialog.isShowing())) {
dialog.show();
}
Log.d(TAG, "Focus Listener " + hasFocus);
}
});
dialog = new DatePickerDialog(MainActivity.this, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH));
private void updateLabel()
{
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
dobET.setText(sdf.format(myCalendar.getTime()));
}
希望这会有所帮助......
答案 3 :(得分: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);
}
}
你完成了:)