我想显示日期选择器弹出窗口。我找到了一些例子,但我没有得到正确的。我有一个edittext,我希望当我点击edittext时,datepicker对话框应该弹出,设置日期后,日期应该显示在edittext在dd/mm/yyyy格式。请为我提供示例代码或良好的链接。
当前回答
我的类为show DatePicker。我可以使用EditText, TextView或按钮
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
其他回答
把这4行放在(EditText, AppCompatEditText)上。它不会显示键盘。然后在clicklistener上,你可以显示datePicker
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
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.Locale;
public class DatePickerHelper {
private final Calendar calendar = Calendar.getInstance();
private final String dateFormat = "MM/dd/yy";
private TextView textView = null;
public DatePickerHelper(final Context context, TextView textView) {
this.textView = textView;
// Setup on click listener if the TextView is not null
if (textView != null) {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDatePickerDialog(context).show();
}
});
}
}
/**
* Return a new date picker listener tied to the specified TextView field
* @return
*/
private DatePickerDialog.OnDateSetListener getOnDateSetListener() {
return new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US);
textView.setText(sdf.format(calendar.getTime()));
}
};
}
/**
* Return new DatePickerDialog for field
* @param context
* @return
*/
private DatePickerDialog getDatePickerDialog(Context context) {
return new DatePickerDialog(context, getOnDateSetListener(), calendar
.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
}
}
用法:
DatePickerHelper assessmentDueDateHelper = new DatePickerHelper(AssessmentsDetailActivity.this,
(TextView) findViewById(R.id.assessmentDueDateEditText));
这是我怎么做的:类似于选择的答案,但使用一个静态对话框类,所以它更可重用,也使用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");
}
}
});
datepicker对话框。
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
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}" />
(参见focusable, inputType和onClick)
在视图模型:
public void onDateEditTextClicked() {
// do something
}
Kotlin端口,调用setDatePicker函数
private fun setDatePicker(dateEditText: EditText) {
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)
updateLabel(myCalendar, dateEditText)
}
dateEditText.setOnClickListener {
DatePickerDialog(this@YourActivityName, datePickerOnDataSetListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show()
}
}
private fun updateLabel(myCalendar: Calendar, dateEditText: EditText) {
val myFormat: String = "dd-MMM-yyyy"
val sdf = SimpleDateFormat(myFormat, Locale.UK)
dateEditText.setText(sdf.format(myCalendar.time))
}
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在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)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?