我想显示日期选择器弹出窗口。我找到了一些例子,但我没有得到正确的。我有一个edittext,我希望当我点击edittext时,datepicker对话框应该弹出,设置日期后,日期应该显示在edittext在dd/mm/yyyy格式。请为我提供示例代码或良好的链接。
当前回答
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;
}
}
}
其他回答
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();
}
}
用这个简单的技巧来做吧:
步骤1:创建一个片段对话框
public class DatePickerFragmentDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
}
}
第二步:在必要的活动中遵循这一点
该活动必须实现:DatePickerDialog。OnDateSetListener 在按钮上设置onClickListener: DialogFragment datePicker = new DatePickerFragmentDialog(); datePicker.show(getSupportFragmentManager(), "自定义日期选择器");
步骤3:覆盖OnDateSetListener
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
@SuppressLint("SimpleDateFormat") DateFormat dateFormat = new
SimpleDateFormat("MM/dd/yyyy");
String currentDateString = dateFormat.format(calendar.getTime());
tvPaymentDate.setText(currentDateString);
}
因此,我们可以使用任何格式的日期:)
使用这个
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();
}
});
还有另一种更好的可重用方式:
创建一个类:
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);
使用片段、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;
}
}
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在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)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?