我想显示日期选择器弹出窗口。我找到了一些例子,但我没有得到正确的。我有一个edittext,我希望当我点击edittext时,datepicker对话框应该弹出,设置日期后,日期应该显示在edittext在dd/mm/yyyy格式。请为我提供示例代码或良好的链接。
当前回答
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))
}
其他回答
使用这个
your_edittext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear+1)
+ "/" + String.valueOf(year);
your_edittext.setText(date);
}
}, yy, mm, dd);
datePicker.show();
}
});
试试这个:
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;
}
}
# 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变量或直接在视图上的文本来获取它。
如果你需要一个单一的功能,所有的活动,当调用将打开日期选择器,然后返回我选择的日期,然后为所有创建一个接口
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();
}
}
使用@DrunkenDaddy扩展函数,至少在我的情况下(以编程方式创建EditText),它在第一次单击时显示键盘,然后在第二次单击时显示日期选择器。
显然,将isFocusable和isFocusableInTouchMode设置为false是无关紧要的,因为第一次点击总是被EditText解释为焦点变化。因此,当EditText获得焦点时,我必须触发单击。现在它像预期的那样工作:第一次点击它直接打开日期选择器,而不是键盘(感谢设置showSoftInputOnFocus为false):
fun EditText.transformIntoDatePicker(context: Context, format: String = "dd/MM/yyyy", maxDate: Date? = null) {
isClickable = true
showSoftInputOnFocus = false
isCursorVisible = false
setOnFocusChangeListener { _, hasFocus -> if (hasFocus) callOnClick()}
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.getDefault())
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()
}
}
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在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)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?