我想显示日期选择器弹出窗口。我找到了一些例子,但我没有得到正确的。我有一个edittext,我希望当我点击edittext时,datepicker对话框应该弹出,设置日期后,日期应该显示在edittext在dd/mm/yyyy格式。请为我提供示例代码或良好的链接。
当前回答
下面是一个针对懒人的Kotlin扩展函数:
fun EditText.transformIntoDatePicker(context: Context, format: String, maxDate: Date? = null) {
isFocusableInTouchMode = false
isClickable = true
isFocusable = false
val myCalendar = Calendar.getInstance()
val datePickerOnDataSetListener =
DatePickerDialog.OnDateSetListener { _, year, monthOfYear, dayOfMonth ->
myCalendar.set(Calendar.YEAR, year)
myCalendar.set(Calendar.MONTH, monthOfYear)
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val sdf = SimpleDateFormat(format, Locale.UK)
setText(sdf.format(myCalendar.time))
}
setOnClickListener {
DatePickerDialog(
context, datePickerOnDataSetListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)
).run {
maxDate?.time?.also { datePicker.maxDate = it }
show()
}
}
}
用法:
在活动:
editText.transformIntoDatePicker(this, "MM/dd/yyyy")
editText.transformIntoDatePicker(this, "MM/dd/yyyy", Date())
在片段:
editText.transformIntoDatePicker(requireContext(), "MM/dd/yyyy")
editText.transformIntoDatePicker(requireContext(), "MM/dd/yyyy", Date())
其他回答
我不能得到这些工作中的任何人,所以将添加我的一个,以防它有帮助。
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"
否则,就像在我的例子中,键盘会不断弹出。
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();
}
});
使用片段、MvvmCross和Xamarin的解决方案。安卓
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;
}
}
如果你需要一个单一的功能,所有的活动,当调用将打开日期选择器,然后返回我选择的日期,然后为所有创建一个接口
public interface OnDatePickerLitionarCallBack {
void DatePicker(String selectedDate);
}
然后在活动中调用接口
public class MapsActivity extends FragmentActivity implements OnDatePickerLitionarCallBack {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
findViewById(R.id.date_pick).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
date_Picker(MapsActivity.this,MapsActivity.this);
}
});
}
@Override
public void DatePicker(String selectedDate) {
}
}
实现接口后,创建一个公共类用于访问任何活动或片段
public class Commons{
public static void date_Picker(Context context, OnDatePickerLitionarCallBack onDatePickerLitionarCallBack) {
final java.util.Calendar myCalendar = java.util.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(java.util.Calendar.YEAR, year);
myCalendar.set(java.util.Calendar.MONTH, monthOfYear);
myCalendar.set(java.util.Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "dd,MMM yyyy"; //In which you need put here
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(myFormat, Locale.US);
onDatePickerLitionarCallBack.DatePicker(sdf.format(myCalendar.getTime()));
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(context, date, myCalendar
.get(java.util.Calendar.YEAR), myCalendar.get(java.util.Calendar.MONTH),
myCalendar.get(java.util.Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
}
试试这个
btn=(Button)findViewById(R.id.chs);
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setCanceledOnTouchOutside(true);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.setContentView(R.layout.datepickerdialog);
dp=(DatePicker) dialog.findViewById(R.id.btp);
dialog.show();
ok=(Button)dialog.findViewById(R.id.btn1);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?
- 如何在Android工作室的外部库中添加一个jar ?
- GridLayout(不是GridView)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?