EditText 上的自定义 DatePicker
Custom DatePicker On EditText
我有 2 个 EditText
.
当有人点击 EditText
日期选择器时,系统会显示日期选择器以在 EditText
中设置日期。所以我对 EditText
的编码是
sdate.setOnTouchListener( new DrawableClickListener.RightDrawableClickListener(sdate)
{
@Override
public boolean onDrawableClick()
{
MyDatePickerDialog(sdate);// put edit text view.
return true;
}
} );
edate.setOnTouchListener( new DrawableClickListener.RightDrawableClickListener(edate)
{
@Override
public boolean onDrawableClick()
{
MyDatePickerDialog(edate);// put edit text view.
return true;
}
} );
我的自定义日期选择器是
private void MyDatePickerDialog(final EditText sdate) {
Toast.makeText(getApplicationContext(),"sdgfdg" , Toast.LENGTH_LONG).show();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
private void updateLabel() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
sdate.setText(sdf.format(myCalendar.getTime()));
}
};
}
但问题是显示了 toast 消息,但它不显示 datepicker
您只创建了 DatePickerDialog 的监听器,而没有创建 DatePickerDialog 本身。
你必须这样做:
DatePickerDialog datePickerDialog = new DatePickerDialog(
getApplicationContext(), your Listener, year, month, dayOfMonth);
datePickerDialog.show();
您只有一个自定义方法,您需要为 OnDateSetListener
创建一个侦听器以传递您的 DatePickerDialog
private void MyDatePickerDialog(final EditText sdate) {
Toast.makeText(getApplicationContext(),"sdgfdg" , Toast.LENGTH_LONG).show();
Calendar newCalendar = Calendar.getInstance();
DatePickerDialog myPickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
updateLabel();
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.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);
sdate.setText(sdf.format(newCalendar.getTime()));
}
myPickerDialog.show();
}
注意: 您可以使用 onClickListener
而不是 onTouchListener
。
我有 2 个 EditText
.
当有人点击 EditText
日期选择器时,系统会显示日期选择器以在 EditText
中设置日期。所以我对 EditText
的编码是
sdate.setOnTouchListener( new DrawableClickListener.RightDrawableClickListener(sdate)
{
@Override
public boolean onDrawableClick()
{
MyDatePickerDialog(sdate);// put edit text view.
return true;
}
} );
edate.setOnTouchListener( new DrawableClickListener.RightDrawableClickListener(edate)
{
@Override
public boolean onDrawableClick()
{
MyDatePickerDialog(edate);// put edit text view.
return true;
}
} );
我的自定义日期选择器是
private void MyDatePickerDialog(final EditText sdate) {
Toast.makeText(getApplicationContext(),"sdgfdg" , Toast.LENGTH_LONG).show();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
private void updateLabel() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
sdate.setText(sdf.format(myCalendar.getTime()));
}
};
}
但问题是显示了 toast 消息,但它不显示 datepicker
您只创建了 DatePickerDialog 的监听器,而没有创建 DatePickerDialog 本身。
你必须这样做:
DatePickerDialog datePickerDialog = new DatePickerDialog(
getApplicationContext(), your Listener, year, month, dayOfMonth);
datePickerDialog.show();
您只有一个自定义方法,您需要为 OnDateSetListener
创建一个侦听器以传递您的 DatePickerDialog
private void MyDatePickerDialog(final EditText sdate) {
Toast.makeText(getApplicationContext(),"sdgfdg" , Toast.LENGTH_LONG).show();
Calendar newCalendar = Calendar.getInstance();
DatePickerDialog myPickerDialog = new DatePickerDialog(this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
updateLabel();
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.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);
sdate.setText(sdf.format(newCalendar.getTime()));
}
myPickerDialog.show();
}
注意: 您可以使用 onClickListener
而不是 onTouchListener
。