MainActivity 代码:

public class MainActivity : Activity
{
TextView _dateDisplay;
Button _dateSelectButton; protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main); _dateDisplay = FindViewById<TextView>(Resource.Id.date_display);
_dateSelectButton = FindViewById<Button>(Resource.Id.date_select_button);
_dateSelectButton.Click += DateSelect_OnClick;
} void DateSelect_OnClick(object sender, EventArgs eventArgs)
{
DatePickerFragment frag = DatePickerFragment.NewInstance(delegate (DateTime time)
{
_dateDisplay.Text = time.ToLongDateString();
});
frag.Show(FragmentManager, DatePickerFragment.TAG);
}
}

DatePickerFragment.cs 代码:

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 + , dayOfMonth);
Log.Debug(TAG, selectedDate.ToLongDateString());
_dateSelectedHandler(selectedDate);
}
}

Main.axml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/top_padding">
<TextView
android:id="@+id/date_display"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:paddingTop="@dimen/top_padding"
android:text="@string/selected_date_text"
style="@android:style/TextAppearance.Large" />
<Button
android:id="@+id/date_select_button"
android:paddingLeft="@dimen/left_padding"
android:paddingRight="@dimen/right_padding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pick_date_text"
android:layout_below="@id/date_display" />
</RelativeLayout>

实现结果:

参考文献:

  https://docs.microsoft.com/zh-cn/xamarin/android/user-interface/controls/pickers/date-picker

最新文章

  1. PHP安装kafka插件
  2. ASP.NET Core--条件处理程序中的依赖注入
  3. OpenGL大作业
  4. [iOS-UI]点击清空按钮,却会有提交的感觉
  5. HashMap的两种遍历方式
  6. Standalone HBase
  7. [GIF] GIF Loop Coder Single Mode
  8. .Net设计模式_单列模式
  9. 异常 ORA-00257: archiver error. Connect internal only, until freed
  10. Extending your SharePoint 2007 site with Microsoft ASP.NET AJAX 3.5
  11. Android开发学习之路--Activity之生命周期
  12. H5页面转成图片并下载到本地
  13. Win Server 2008 r2 with SP2装机过程记录
  14. 前端异步技术之Promise
  15. swust oj 983
  16. [Java concurrent][Collections]
  17. unity DoTween使用
  18. 前端单页面富应用(SPA)的实现
  19. @Autowired与@Resource 详细诠释和区别(附带例子)
  20. NOI 8785 装箱问题(0-1背包)

热门文章

  1. 一些质量极高的project-based tutorials
  2. Vue-cli构建spa应用
  3. c# 读取数据库得到字符串
  4. linux pid文件
  5. Ingress-Nginx
  6. 安装docker后,导致qemu的桥接网络出现问题
  7. Mysql数据库之慢查询
  8. [windows]c盘瘦身、系统盘清理临时文件、缓存等垃圾文件
  9. eclipse IDE for java developers下载与安装
  10. 2.搭建第一个http服务:三层架构