我正在使用Xamarin开发Android应用程序,并希望使用datePicker在editText字段上设置日期。当我们点击editText时,datePicker会打开,但是当设置了日期时,日期不会显示在editText上。提供的代码有什么问题。
提前致谢。
public class PageFragment : Android.Support.V4.App.Fragment
{
int _num;
database baseObj = new database();
EditText datePickerText;
DatePickerDialog Date;
View v;
public PageFragment()
{
}
public PageFragment(int num)
{
// Supply num input as an argument.
var args = new Bundle();
args.PutInt("num", num);
Arguments = args;
}
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
_num = Arguments != null ? Arguments.GetInt("num") : 1;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
v = inflater.Inflate(Resource.Layout.distribution, container, false);
datePickerText = v.FindViewById<EditText>(Resource.Id.editTextCalender);
datePickerText.Focusable = false;
datePickerText.Click += delegate
{
var dialog = new DatePickerDialogFragment(Activity, DateTime.Now, null);
dialog.Show(FragmentManager, "date");
};
return v;
}
public void OnDateSet(Android.Widget.DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
v.FindViewById<EditText>(Resource.Id.editTextCalender).Text = new DateTime(year, monthOfYear + 1, dayOfMonth).ToString();
}
public 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;
}
}
}
答案 0 :(得分:0)
我知道回答你的问题已经很晚了。希望你已经解决了你的问题。我得到了为什么editTextCalender无法显示您通过DatePickerDialog设置的日期的答案。你必须改变以下代码行
public void OnDateSet(Android.Widget.DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
v.FindViewById<EditText>(Resource.Id.editTextCalender).Text = new DateTime(year, monthOfYear + 1, dayOfMonth).ToString();
}
进入(纠正一个)
public void OnDateSet(Android.Widget.DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
datePickerText.Text = new DateTime(year, monthOfYear + 1, dayOfMonth).ToString();
}
因为在处理点击事件之前,您在创建视图时已经夸大了datePickerText = v.FindViewById<EditText>(Resource.Id.editTextCalender);
。
谢谢!