Datepicker:如何在点击edittext时popupdatepicker

我想显示dateselect器popup窗口。 我find了一些例子,但我没有得到正确的。 我有一个编辑文本,我希望当我点击edittext的dateselect器对话框应该popup,设置date后,date应该以dd / mm / yyyy格式edittext中显示。 PLease提供示例代码或良好的链接。

在XML中试试这个:

<EditText android:id="@+id/Birthday" custom:font="@string/font_avenir_book" android:clickable="true" android:editable="false" android:hint="@string/birthday"/> 

现在在Java文件中:

 Calendar myCalendar = Calendar.getInstance(); EditText edittext= (EditText) findViewById(R.id.Birthday); 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(); } }; edittext.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new DatePickerDialog(classname.this, date, myCalendar .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show(); } }); 

现在在上面的活动中添加方法。

  private void updateLabel() { String myFormat = "MM/dd/yy"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); edittext.setText(sdf.format(myCalendar.getTime())); } 

在EditText的xml文件中添加android:focusable="false"以允许一次触摸。

无法得到这些工作中的任何人,所以将添加我的一个,以防万一它有帮助。

 public class MyEditTextDatePicker implements OnClickListener, OnDateSetListener { EditText _editText; private int _day; private int _month; private int _birthYear; private Context _context; public MyEditTextDatePicker(Context context, int editTextViewID) { Activity act = (Activity)context; this._editText = (EditText)act.findViewById(editTextViewID); this._editText.setOnClickListener(this); this._context = context; } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { _birthYear = year; _month = monthOfYear; _day = dayOfMonth; updateDisplay(); } @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); DatePickerDialog dialog = new DatePickerDialog(_context, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); dialog.show(); } // updates the date in the birth date EditText private void updateDisplay() { _editText.setText(new StringBuilder() // Month is 0 based so add 1 .append(_day).append("/").append(_month + 1).append("/").append(_birthYear).append(" ")); } } 

还有一些是别人没有提到的。 确保你把下面的EditText XML。

 android:focusable="false" 

否则就像在我的情况下,键盘将不断popup。 希望这有助于某人

 class MyClass implements OnClickListener, OnDateSetListener { EditText editText; this.editText = (EditText) findViewById(R.id.editText); this.editText.setOnClickListener(this); @Override public void onClick(View v) { DatePickerDialog dialog = new DatePickerDialog(this, this, 2013, 2, 18); dialog.show(); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // this.editText.setText(); } } 

还有一个更好的可重用的方法:

创build一个类:

 class setDate implements OnFocusChangeListener, OnDateSetListener { private EditText editText; private Calendar myCalendar; public setDate(EditText editText, Context ctx){ this.editText = editText; this.editText.setOnFocusChangeListener(this); myCalendar = Calendar.getInstance(); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // this.editText.setText(); String myFormat = "MMM dd, yyyy"; //In which you need put here SimpleDateFormat sdformat = new SimpleDateFormat(myFormat, Locale.US); myCalendar.set(Calendar.YEAR, year); myCalendar.set(Calendar.MONTH, monthOfYear); myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); editText.setText(sdformat.format(myCalendar.getTime())); } @Override public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub if(hasFocus){ new DatePickerDialog(ctx, this, myCalendar .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show(); } } } 

然后在onCreate函数下调用这个类:

  EditText editTextFromDate = (EditText) findViewById(R.id.editTextFromDate); setDate fromDate = new setDate(editTextFromDate, this); 

这是我如何做的:类似于所选的答案,但使用静态对话框类,所以它更可重用,也使用onFocusChange而不是onClick。

 editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { DialogFragment datePickerFragment = new DatePickerFragment() { @Override public void onDateSet(DatePicker view, int year, int month, int day) { Log.d(TAG, "onDateSet"); Calendar c = Calendar.getInstance(); c.set(year, month, day); editText.setText(df.format(c.getTime())); nextField.requestFocus(); //moves the focus to something else after dialog is closed } }; datePickerFragment.show(getActivity().getSupportFragmentManager(), "datePicker"); } } }); 

和dateselect器对话框。

 public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker 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); // 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) { //blah } } 

所选答案对我来说不太合适,因为我不得不点击EditText框一次,然后在OnClickListener触发之前再次点击它。 我可以通过用OnTouchListenerreplaceOnClickListener来解决这个问题,以防万一任何人遇到类似的问题,这就是我的代码:

 Calendar myCalendar = Calendar.getInstance(); 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(); } }; edittext.setOnTouchListener(new View.OnTouchListener() { @Override public void onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { new DatePickerDialog(classname.this, date, myCalendar .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show(); } } }); private void updateLabel() { String myFormat = "MM/dd/yy"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); edittext.setText(sdf.format(myCalendar.getTime())); } 

#1。 我想显示一个datepicker当前date预选 (我使用butterknife注射)

  @OnClick(R.id.your_view) public void onClickYourView() { final Calendar myCalendar = Calendar.getInstance(); 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); String myFormat = "dd MMMM yyyy"; // your format SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault()); your_view.setText(sdf.format(myCalendar.getTime())); } }; new DatePickerDialog(mContext, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH)).show(); } 

2.如果要预先selectdate,请用最后一个部分replace

 new DatePickerDialog(mContext, date, 1990, 0, 1).show(); 

那么你可以使用myCalendarvariables或直接在你的视图上的文本。

使用碎片,MvvmCross和Xamarin.Android的解决scheme

 public class EnterTimeView : MvxFragment, DatePickerDialog.IOnDateSetListener { private EditText datePickerText; public EnterTimeView() { this.RetainInstance = true; } public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState) { this.HasOptionsMenu = true; var ignored = base.OnCreateView(inflater, container, savedInstanceState); var view = inflater.Inflate(Resource.Layout.EnterTimeView, container, false); datePickerText = view.FindViewById<EditText>(Resource.Id.DatePickerEditText); datePickerText.Focusable = false; datePickerText.Click += delegate { var dialog = new DatePickerDialogFragment(Activity, Convert.ToDateTime(datePickerText.Text), this); dialog.Show(FragmentManager, "date"); }; var set = this.CreateBindingSet<EnterTimeView, EnterTimeViewModel>(); set.Bind(datePickerText).To(vm => vm.Date); set.Apply(); return view; } public void OnDateSet(Android.Widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) { datePickerText.Text = new DateTime(year, monthOfYear + 1, dayOfMonth).ToString(); } private class DatePickerDialogFragment : Android.Support.V4.App.DialogFragment { private readonly Context _context; private DateTime _date; private readonly DatePickerDialog.IOnDateSetListener _listener; public DatePickerDialogFragment(Context context, DateTime date, DatePickerDialog.IOnDateSetListener listener) { _context = context; _date = date; _listener = listener; } public override Dialog OnCreateDialog(Bundle savedState) { var dialog = new DatePickerDialog(_context, _listener, _date.Year, _date.Month - 1, _date.Day); return dialog; } } 
  selectDate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DatePickerDialog mdiDialog =new DatePickerDialog(mContext,new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(getApplicationContext(),year+ " "+monthOfYear+" "+dayOfMonth,Toast.LENGTH_LONG).show(); } }, year, month, date); mdiDialog.show(); } }); 

我已经尝试了所有的build议,但他们都有自己的问题。

我认为最好的解决办法是使用如下的框架布局:

 <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" .... /> <View android:id="@+id/invisible_click_watcher" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" /> </FrameLayout> 

然后添加@Android答案中精美的DatePickerDialog代码。

为我工作得很好

我在EditText视图中定义

 android:focusable="false" 

我的class表演DatePicker 。 我可以使用EditTextTextViewButton

 import android.app.DatePickerDialog; import android.content.Context; import android.view.View; import android.widget.DatePicker; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class TextViewDatePicker implements View.OnClickListener, DatePickerDialog.OnDateSetListener { public static final String DATE_SERVER_PATTERN = "yyyy-MM-dd"; private DatePickerDialog mDatePickerDialog; private TextView mView; private Context mContext; private long mMinDate; private long mMaxDate; public TextViewDatePicker(Context context, TextView view) { this(context, view, 0, 0); } public TextViewDatePicker(Context context, TextView view, long minDate, long maxDate) { mView = view; mView.setOnClickListener(this); mView.setFocusable(false); mContext = context; mMinDate = minDate; mMaxDate = maxDate; } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, monthOfYear); calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); Date date = calendar.getTime(); SimpleDateFormat formatter = new SimpleDateFormat(DATE_SERVER_PATTERN); mView.setText(formatter.format(date)); } @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); mDatePickerDialog = new DatePickerDialog(mContext, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); if (mMinDate != 0) { mDatePickerDialog.getDatePicker().setMinDate(mMinDate); } if (mMaxDate != 0) { mDatePickerDialog.getDatePicker().setMaxDate(mMaxDate); } mDatePickerDialog.show(); } public DatePickerDialog getDatePickerDialog() { return mDatePickerDialog; } public void setMinDate(long minDate) { mMinDate = minDate; } public void setMaxDate(long maxDate) { mMaxDate = maxDate; } } 

运用

 EditText myEditText = findViewById(R.id.myEditText); TextViewDatePicker editTextDatePicker = new TextViewDatePicker(context, myEditText, minDate, maxDate); //TextViewDatePicker editTextDatePicker = new TextViewDatePicker(context, myEditText); //without min date, max date 
 public class DatePickerActivity extends AppCompatActivity { Button button; static TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button= (Button) findViewById(R.id.btn_click); textView= (TextView) findViewById(R.id.txt_date); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { DialogFragment newFragment=new DatePickerFragment(); newFragment.show(getFragmentManager(), "datepicker"); } }); } public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{ @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int day) { String years=""+year; String months=""+(monthOfYear+1); String days=""+day; if(monthOfYear>=0 && monthOfYear<9){ months="0"+(monthOfYear+1); } if(day>0 && day<10){ days="0"+day; } textView.setText(days+"/"+months+"/"+years); } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { //use the current date as the default date in the picker 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); DatePickerDialog datePickerDialog=null; datePickerDialog=new DatePickerDialog(getActivity(), this, year, month, day); return datePickerDialog; } } } 

使用DataBinding:

 <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:onClick="@{() -> viewModel.onDateEditTextClicked()}" android:hint="@string/hint_date" android:imeOptions="actionDone" android:inputType="none" android:maxLines="1" android:text="@={viewModel.filterDate}" /> 

(请参阅focusableinputTypeonClick

内部ViewModel:

 public void onDateEditTextClicked() { // do something } 

尝试这个:

 public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { DataPickerListener listener; public DatePickerFragment(DataPickerListener l) { listener = l; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the date picker 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(), AlertDialog.THEME_HOLO_LIGHT, this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { month = month + 1; String stringOfDate = day + "-" + month + "-" + year; listener.setDate(stringOfDate); Util.Log(stringOfDate); } @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub super.onCancel(dialog); listener.cancel(); } public static interface DataPickerListener { void setDate(String date); void cancel(); } } // use this function to open datePicker popup DialogFragment newFragment; public void pickDate() { if (newFragment == null) newFragment = new DatePickerFragment(new PickDateListener()); newFragment.show(getFragmentManager(), "Date Picker"); } class PickDateListener implements DataPickerListener { @Override public void setDate(String date) { createSpiner(date); onetouch = false; } @Override public void cancel() { onetouch = false; } } 
  EditText text=(EditText)findViewById(R.id.editText); text.setOnClickListener(new OnClickListener(){ @RequiresApi(api = Build.VERSION_CODES.N) @Override public void onClick(View view) { DatePickerDialoge datePickerDialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { String date = i2 + "/" + (++i1) + "/" + i; text.setText(date); } },year,month,date); } }); datePickerDialog.show(); 

您还可以通过这些语句设置最短date和最长date。

  datepickerDialoge.getDatepicker().setMinDate(long Date); datepickerDialoge.getDatepicker().setMaxDate(long Date); 

注意:在datepickerDiloge.show()之前添加这些行; 声明你会得到这样的date= 12/2/2017。

我希望我的回答会有所帮助。

不要忘了设置编辑文本的属性如下:

 android:clickable="true" android:focusable="false" 

我认为最好重写EditText类…

 public class EditTextDP extends android.support.v7.widget.AppCompatEditText implements View.OnClickListener, DatePickerDialog.OnDateSetListener { public EditTextDP(Context context) { super(context); setOnClickListener(this); setFocusable(false); } public EditTextDP(Context context, AttributeSet attrs) { super(context, attrs); setOnClickListener(this); setFocusable(false); } public EditTextDP(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setOnClickListener(this); setFocusable(false); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { setText(new StringBuilder().append(dayOfMonth).append("/").append(monthOfYear + 1).append("/").append(year)); } @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(TimeZone.getDefault()); DatePickerDialog dialog = new DatePickerDialog(getContext(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); dialog.show(); } } 

用XML代替…

  <*.*.*.EditTextDP android:id="@+id/c1_dob_hm" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/round_edittext_white" android:ems="10" android:padding="8dp"> </*.*.*.EditTextDP>