[英]Display timerpickerdialog within a fragment using Xamarin Android
我已經按照xamarin Timepicker教程鏈接在Activity
創建Timepickerdialog
。 效果很好。
我需要在一個fragment
執行相同的操作,但是未能使其正常工作。 有人可以告訴我如何使用片段嗎? 任何公會或鏈接或建議。
我正在使用Xamarin Andriod
。 在此先感謝您。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/timeDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
<Button android:id="@+id/pickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change the time"/>
</LinearLayout>
MainActivity.cs
private TextView time_display;
private Button pick_button;
private int hour;
private int minute;
const int TIME_DIALOG_ID = 0;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "Main" layout resource
SetContentView (Resource.Layout.Main);
// Capture our View elements
time_display = FindViewById<TextView> (Resource.Id.timeDisplay);
pick_button = FindViewById<Button> (Resource.Id.pickTime);
// Add a click listener to the button
pick_button.Click += (o, e) => ShowDialog (TIME_DIALOG_ID);
// Get the current time
hour = DateTime.Now.Hour;
minute = DateTime.Now.Minute;
// Display the current date
UpdateDisplay ();
}
// Updates the time we display in the TextView
private void UpdateDisplay ()
{
string time = string.Format ("{0}:{1}", hour, minute.ToString ().PadLeft (2, '0'));
time_display.Text = time;
}
private void TimePickerCallback (object sender, TimePickerDialog.TimeSetEventArgs e)
{
hour = e.HourOfDay;
minute = e.Minute;
UpdateDisplay ();
}
protected override Dialog OnCreateDialog (int id)
{
if (id == TIME_DIALOG_ID)
return new TimePickerDialog (this, TimePickerCallback, hour, minute, false);
return null;
}
Fragment.cs
//Everything same as Activity
//updated the following:
time_display = view.FindViewById<TextView>(Resource.Id.timeDisplay);
pick_button = view.FindViewById<Button>(Resource.Id.pickTime);
protected override Dialog OnCreateDialog(int id)
{
if (id == TIME_DIALOG_ID)
return new TimePickerDialog(this.Activity, TimePickerCallback, hour, minute, false);
return null;
}
結果:
錯誤1.名稱'ShowDialog'在當前上下文中不存在。
錯誤2.Fragement.OnCreateDialog(int)':找不到適合的方法來覆蓋。
嘗試使用這組代碼來顯示TimePickerDialog。
TimePickerDialog timePickerDialog = new TimePickerDialog(this.Activity, TimePickerCallback, hour, minute, false);
timePickerDialog.Show();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.