您好我在本机日历视图的日历日期显示点或文字时遇到问题,我尝试过但Xamarin原生没有可用的代码,所以请帮我解决这个问题。
以下是我的代码:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.CalendarView);
// Create your application here
calendar = FindViewById<CalendarView>(Resource.Id.calendar);
calendar.DateChange += CalendarOnDateChange;
}
#region Set Date in DatePicker
void DateSelect_Start(object sender, EventArgs eventArgs)
{
DatePickerFragment start = DatePickerFragment.NewInstance(delegate (DateTime time)
{
_dateDisplayStart.Text = time.Date.ToString("d");
_dateDisplayEnd.Text = time.Date.ToString("d");
});
start.Show(FragmentManager, DatePickerFragment.TAG);
}
void DateSelect_End(object sender, EventArgs eventArgs)
{
DatePickerFragment end = DatePickerFragment.NewInstance(delegate (DateTime time)
{
_dateDisplayEnd.Text = time.Date.ToString("d");
});
end.Show(FragmentManager, DatePickerFragment.TAG);
}
#endregion
private void CalendarOnDateChange(object sender, CalendarView.DateChangeEventArgs args)
{
newdatetime = new DateTime(args.Year, args.Month + 1, args.DayOfMonth);
TextViewCalendar = FindViewById<TextView>(Resource.Id.TextViewCalendar);
//tv.Text = "Schedule for Date: " + newdatetime.ToString();
builder = new Android.Support.V7.App.AlertDialog.Builder(this);
var dialogView = LayoutInflater.Inflate(Resource.Layout.CalendarPopup, null);
builder.SetView(dialogView);
Button bt_save = dialogView.FindViewById<Button>(Resource.Id.button_save);
Button bt_cancel = dialogView.FindViewById<Button>(Resource.Id.button_cancel);
tx_start = dialogView.FindViewById<TextView>(Resource.Id.textView_date);
tx_end = dialogView.FindViewById<TextView>(Resource.Id.textView_enddate);
ed_popup = dialogView.FindViewById<EditText>(Resource.Id.editText_Title);
_dateDisplayStart = dialogView.FindViewById<TextView>(Resource.Id.textView_date);
_dateSelectButtonStart = dialogView.FindViewById<ImageView>(Resource.Id.imageView_calendar);
_dateSelectButtonStart.Click += DateSelect_Start;
_dateDisplayEnd = dialogView.FindViewById<TextView>(Resource.Id.textView_enddate);
_dateSelectButtonEnd = dialogView.FindViewById<ImageView>(Resource.Id.imageView_calendartwo);
_dateSelectButtonEnd.Click += DateSelect_End;
bt_save.Click += SaveChanges;
bt_cancel.Click += CancelChanges;
tx_start.Text = newdatetime.Date.ToString("d");
tx_end.Text = newdatetime.Date.ToString("d");
alert = builder.Create();
alert.Show();
}
void SaveChanges(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(ed_popup.Text))
{
ed_popup.Error = "Leave reason";
}
else
{
string textCalendar = ed_popup.Text.ToString() + " " + newdatetime.Date.ToString("d");
TextViewCalendar.Text = textCalendar;
alert.Dismiss();
}
}
void CancelChanges(object sender, EventArgs e)
{
alert.Dismiss();
}
public class DatePickerFragment : DialogFragment,
DatePickerDialog.IOnDateSetListener
{
// TAG can be any string of your choice.
public static readonly string TAG = "X:" + typeof(DatePickerFragment).Name.ToUpper();
// Initialize this value to prevent NullReferenceExceptions.
Action<DateTime> _dateSelectedHandler = delegate { };
public static DatePickerFragment NewInstance(Action<DateTime> onDateSelected)
{
DatePickerFragment frag = new DatePickerFragment();
frag._dateSelectedHandler = onDateSelected;
return frag;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
DateTime currently = DateTime.Now;
DatePickerDialog dialog = new DatePickerDialog(Activity, this, currently.Year, currently.Month, currently.Day);
return dialog;
}
public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
// Note: monthOfYear is a value between 0 and 11, not 1 and 12!
DateTime selectedDate = new DateTime(year, monthOfYear + 1, dayOfMonth);
Android.Util.Log.Debug(TAG, selectedDate.ToLongDateString());
_dateSelectedHandler(selectedDate);
}
}
}
和布局代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f1e8"
android:orientation="vertical">
<CalendarView
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="350dp"
android:background="#ffffff"
android:theme="@android:style/Theme.Holo.Light.DarkActionBar" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/calendar"
android:background="#db9b55" />
<TextView
android:id="@+id/TextViewCalendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textColor="#000000"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp" />
<ListView
android:id="@+id/list_calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
答案 0 :(得分:1)
请在下面找到添加约会的代码,这在Xamarin.Forms中使用。您可以指定应用Context
。
public void AddAppointment(string title, string description, DateTime startDate, DateTime endDate)
{
Intent intent = new Intent(Intent.ActionInsert);
intent.PutExtra(CalendarContract.Events.InterfaceConsts.Title, title);
intent.PutExtra(CalendarContract.Events.InterfaceConsts.Description, description);
intent.PutExtra(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMs(startDate));
intent.PutExtra(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMs(endDate));
intent.PutExtra(CalendarContract.ExtraEventBeginTime, GetDateTimeMs(startDate));
intent.PutExtra(CalendarContract.ExtraEventEndTime, GetDateTimeMs(endDate));
intent.PutExtra(CalendarContract.Events.InterfaceConsts.EventTimezone, "UTC");
intent.PutExtra(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "UTC");
intent.SetData(CalendarContract.Events.ContentUri);
((Activity)Forms.Context).StartActivity(intent);
}
编辑1 (根据OP的新要求,在应用中显示带有事件的原生日历):
CalendarView不支持在其中显示事件。您可以阅读有关创建自定义日历视图以及在SO问题中添加功能的更多信息 - Android CalendarView for Showing Events。
您可以使用ExtendedCalendarView Java项目作为基础并创建代码的C#版本。
目前没有简单的方法来展示具有该功能的日历 为了在天上显示事件,ExtendedCalendarView旨在解决这个问题 问题