我想显示日期选择器弹出窗口。我找到了一些例子,但我没有得到正确的。我有一个edittext,我希望当我点击edittext时,datepicker对话框应该弹出,设置日期后,日期应该显示在edittext在dd/mm/yyyy格式。请为我提供示例代码或良好的链接。
当前回答
下面是Material Date Picker的Kotlin扩展函数:
fun TextInputEditText.datePicker(fm:FragmentManager, tag: String) {
isFocusableInTouchMode = false
isClickable = true
isFocusable = false
val datePicker =
MaterialDatePicker.Builder.datePicker()
.setTitleText("Select Date")
.build()
setOnClickListener {
datePicker.show(fm,"tag")
}
datePicker.addOnPositiveButtonClickListener {
setText(datePicker.headerText)
Log.d("TAG", "datePicker: \n $it")
}}
用法:
在活动:
yourTextInputEditText.datePicker(supportFragmentManager,"tag")
在片段:
yourTextInputEditText.datePicker(requireActivity().supportFragmentManager,"tag")
其他回答
这在2020年1月对我有效
XML部分:
<EditText
android:id="@+id/etDOB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:editable="false"
android:ems="10"
android:hint="Enter Your Date of Birth" />
Java的部分:
final Calendar myCalendar = Calendar.getInstance();
userDOBView = (EditText)findViewById(R.id.etDOB);
final 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();
}
};
userDOBView.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
new DatePickerDialog("YourClassName".this, date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
return true;
}
});
private void updateLabel() {
String myFormat = "MM/dd/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
// edittext.setText(sdf.format(myCalendar.getTime()));
userDOBView.setText(sdf.format(myCalendar.getTime()));
}
还有另一种更好的可重用方式:
创建一个类:
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);
日期选择对话框
Calendar c=Calendar.getInstance();
Integer month=c.get(Calendar.MONTH);
Integer day=c.get(Calendar.DAY_OF_MONTH);
Integer year=c.get(Calendar.YEAR);
DatePickerDialog datePickerDialog =new DatePickerDialog(Expenture_Activity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
et_from.setText(dayOfMonth+"/"+month+"/"+year);
}
},year,month,day);
datePickerDialog.show();
# 1。我想显示一个datepicker与当前日期预选(我使用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. 如果您想预选日期,请将最后一部分替换为
new DatePickerDialog(mContext, date, 1990, 0, 1).show();
然后你可以使用myCalendar变量或直接在视图上的文本来获取它。
下面是一个针对懒人的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())
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在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)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?