目前,datepicker以m / d / yyyy格式将日期设置为textview。但我想将dateformat从m / d / yyyy更改为dd-mm-yyyy。我搜索了这个主题。但是没有找到解决方案。感谢任何帮助。
Main.axml
<TextView
android:layout_weight="80"
android:id="@+id/dateDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date" />
<Button
android:layout_weight="10"
android:id="@+id/pickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick date" />
mainactivity.cs
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Create your application here
SetContentView(Resource.Layout.Main);
dateDisplay = FindViewById<TextView> (Resource.Id.dateDisplay);
pickDate = FindViewById<Button> (Resource.Id.pickDate);
// add a click event handler to the button
pickDate.Click += delegate { ShowDatePickerDialog (); };
// get the current date
//date = DateTime.Today;
// display the current date (this method is below)
// UpdateDisplay (date);
}
void ShowDatePickerDialog()
{
var dialog = new DatePickerFragment(this, DateTime.Now, this);
dialog.Show(FragmentManager, null);
}
public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
var newDate = new DateTime(year, monthOfYear + 1, dayOfMonth);
UpdateDisplay(newDate);
}
void UpdateDisplay(DateTime selectedDate)
{
// selectedDate.GetDateTimeFormats('d');
// dateDisplay.Text = selectedDate.GetDateTimeFormats('d').ToString();
dateDisplay.Text = selectedDate.ToString("d");
}
}
DatePickerFragment.cs
public class DatePickerFragment : DialogFragment
{
private readonly Context context;
private DateTime date;
private readonly DatePickerDialog.IOnDateSetListener listener;
public DatePickerFragment(Context context, DateTime date, DatePickerDialog.IOnDateSetListener listener)
{
this.context = context;
this.date = date;
this.listener = listener;
}
public override Dialog OnCreateDialog(Bundle savedState)
{
var dialog = new DatePickerDialog(context, listener, date.Year, date.Month - 1, date.Day);
return dialog;
}
}
答案 0 :(得分:4)
在UpdateDisplay
:
dateDisplay.Text = selectedDate.ToString("dd-MM-yyyy");