使用C# + Xamarin开发Android 应用 -- Datetime Picker

1. axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout4">
        <TextView
            android:text="Service Name"
            android:layout_width="400px"
            android:layout_height="match_parent"
            android:id="@+id/lblBdServiceName" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1">
        <Button
            android:text="Select Date"
            android:id="@+id/btnBDChooseDate"
            android:layout_width="154.5dp"
            android:layout_height="match_parent" />
        <TextView
            android:text=""
            android:layout_width="400px"
            android:layout_height="match_parent"
            android:id="@+id/lblBDDate" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout2">
        <Button
            android:text="Select Time"
            android:id="@+id/btnBDChooseTime"
            android:layout_width="154.5dp"
            android:layout_height="match_parent" />
        <TextView
            android:text=""
            android:layout_width="400px"
            android:layout_height="match_parent"
            android:id="@+id/lblBDTime" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3">
        <TextView
            android:layout_width="154.5dp"
            android:layout_height="match_parent"
            android:id="@+id/lblEmpty" />
        <Button
            android:text="Confirm"
            android:id="@+id/btnBDConfirm"
            android:layout_width="154.5dp"
            android:layout_height="match_parent"
            android:layout_marginRight="320.8dp" />
    </LinearLayout>
</LinearLayout>

2. Activity.cs

public class BookingDetails : Activity
    {

        private TextView timeDisplay;
        private Button pickTime;
        private int hour;
        private int minute;
        const int TIME_DIALOG_ID = 0;

        private TextView dateDisplay;
        private Button pickDate;
        private DateTime date;
        const int DATE_DIALOG_ID = 1;

        private DateTime serverDateTime ;

        protected override void OnCreate(Bundle bundle)
        {
            SetContentView(Resource.Layout.BookingDetails);
            base.OnCreate(bundle);

            try
            {
                var lblServiceName = FindViewById<TextView>(Resource.Id.lblBdServiceName);
                lblServiceName.SetText("Service 1", TextView.BufferType.Normal);

                serverDateTime = DateTime.Parse("2015-5-22 15:30:26");
                // Create your application here

                timeDisplay = FindViewById<TextView>(Resource.Id.lblBDTime);
                pickTime = FindViewById<Button>(Resource.Id.btnBDChooseTime);

                // Add a click listener to the button
                pickTime.Click += (o, e) => ShowDialog(TIME_DIALOG_ID);

                hour = serverDateTime.Hour;
                minute = serverDateTime.Minute;
                UpdateDisplayTime();

                // capture our View elements
                dateDisplay = FindViewById<TextView>(Resource.Id.lblBDDate);
                pickDate = FindViewById<Button>(Resource.Id.btnBDChooseDate);

                // add a click event handler to the button
                pickDate.Click += delegate { ShowDialog(DATE_DIALOG_ID); };

                date = serverDateTime;
                UpdateDisplayDate();

                var btnCfm = FindViewById<Button>(Resource.Id.btnBDConfirm);
                btnCfm.Click += (sender, args) =>
                {
                    try
                    {
                        var serviceName = FindViewById<TextView>(Resource.Id.lblBdServiceName).Text;
                        var selectDate = FindViewById<TextView>(Resource.Id.lblBDDate).Text;
                        var selectTime = FindViewById<TextView>(Resource.Id.lblBDTime).Text;

                        var selectedDateTime =
                           DateTime.Parse(string.Format("{0} {1}", selectDate, selectTime)).ToString();
                        var msg = string.Format("Confirm booking service ‘{0}‘ at {1} ?", serviceName, selectedDateTime);
                        this.ShowDlgYesNo(msg, "Booking Confirmation",
                            (s, arg) =>
                            {
                                this.ShowAlert("call api book");
                                this.GotoActicity<MainActivity>();
                            },
                            (s, arg) =>
                            {
                                // do nothing
                            });
                    }
                    catch (Exception ex)
                    {
                        this.ShowAlert(ex.Message);
                    }

                };
            }
            catch (Exception ex)
            {
                this.ShowAlert(ex.Message);
            }

        }

        private void UpdateDisplayTime()
        {
            string time = string.Format("{0}:{1}", hour, minute.ToString().PadLeft(2, ‘0‘));
            timeDisplay.Text = time;

        }

        private void UpdateDisplayDate()
        {
            dateDisplay.Text = date.ToString("d");
        }

        private void TimePickerCallback(object sender, TimePickerDialog.TimeSetEventArgs e)
        {
            hour = e.HourOfDay;
            minute = e.Minute;
            UpdateDisplayTime();
        }

        void OnDateSet(object sender, DatePickerDialog.DateSetEventArgs e)
        {
            this.date = e.Date;
            UpdateDisplayDate();
        }

        protected override Dialog OnCreateDialog(int id)
        {
            if (id == TIME_DIALOG_ID)
            {
                return new TimePickerDialog(this, TimePickerCallback, hour, minute, false);
            }
            if (id == DATE_DIALOG_ID)
            {
                var dlg = new DatePickerDialog(this, OnDateSet, serverDateTime.Year, serverDateTime.Month-1, serverDateTime.Day);
                return dlg;
            }

            return null;
        }
    }
时间: 2024-08-28 10:34:36

使用C# + Xamarin开发Android 应用 -- Datetime Picker的相关文章

Xamarin开发Android时Visual Studio 2012没有智能提示解决办法

Most of the people who work with Xamarin’s Mono for Android in Visual Studio 2012 face a bug where Intellisense doesn’t work for AXML in source view. One of the fix which worked for me is mentioned below. Launch Visual Studio 2012 Open a solution wit

Xamarin开发Android应用打包apk

Visual Studio中用Xamarin开发Android应用,生成apk文件有3种方法 1.debug时,代码目录下bin\Debug中会自动生成调试用***-Signed.apk文件,但是文件Size非常大,不建议使用. 2.Release时,“部署”勾选√,生成--部署解决方案后,代码目录下bin\Release中会自动生成部署用***-Signed.apk文件,文件Size比较小. 3.自己打包,工具--Publish Android Application,一步一步设置打包,文件S

【Xamarin开发 Android 系列 6】 Android 结构基础(上)

前面大家已经熟悉了什么是Android,而且在 [Xamarin开发 Android 系列 4] Android 基础知识 一文里,开篇就上图,展示了Android的架构. 大概也就4层 APP--应用程序层 Framework --公共架构层 Native Libs + Dalvik VM---系统类库+虚拟机 Linux Kernel--Linux内核 Android 是一个基于Linux开发的可以运行在智能手机 平板电脑设备上 的系统.如 Android4.0 基于的是Linux2.6 内

C#使用Xamarin开发Android应用程序 -- 系列文章

Xamarin开发Android应用程序 利用Xamaria构建Android应用-公交发车信息屏 Xamarin版的C# SVG路径解析器 C#使用Xamarin开发可移植移动应用(1.入门与Xamarin.Forms页面),附源码 为 Xamarin.Forms 做个跑马灯控件 [Xamarin挖墙脚系列:现有IPhone/IPad 设备尺寸] [Xamarin挖墙脚系列:IOS-关于手机支持的屏幕方向] [Xamarin挖墙脚系列:Xamarin.IOS机制原理剖析] [Xamarin挖墙

xamarin开发android收集的一些工具

xamarin开发android收集的一些工具 工欲善其事,必先利其器,从16年下半年开始做xamarin相关的开发,平时使用的一些工具和google插件给大家分享一下,都有下载地址,持续更新. Visual Studio Emulator for Android 通过 Visual Studio 实现最佳Android模拟器(微软家的) Genymotion Android模拟器(google家的) Xamarin Live Player xamarin开发android.ios实时预览器 C

Xamarin开发Android笔记:使用ZXing进行连续扫描

在项目开发中需要使用到条码扫描,因为以前就测试过ZXing,感觉识别速度和功能都不错,所以直接引用.不过在实际开发的过程中,却遇到连续扫描的问题,每次扫描识别完成之后,扫描窗体自动关闭了. 在Xamarin论坛中查找解决方案,只是找到的iOS版本的解决方案.参考iOS的解决方案,其实就是在扫描完成之后重新打开扫描.按照这个思路,想到使用Intent for result的方式来进行实现.实现方法如下代码: 主窗体: 1 using System; 2 using Android.App; 3 u

【Xamarin开发 Android 系列 2】VS2015跨平台开发的几种方式

在微软Build大会上,微软宣布在VS2015中支持三种方式进行跨平台的开发. 1. Xamarin 2. Cordova 3. C++ Xamarin  官网 :http://xamarin.com/ Xamarin 是由早期的Mono项目演变而来,原本是在Linux上执行C#程序的一个开放原始码项目.后来陆续发表支持iOS的Mono Touch framework以及Mono For Android Framework.因此它在市场上并不是一个新的产品.2011年,原本在Novell的Mon

【Xamarin开发 Android 系列 7】 Android 结构基础(下)

*******前期我们不打算进行太深入的东西,省的吓跑刚进门的,感觉门槛高,so,我们一开始就是跑马灯一样,向前蹿************* 前一篇 我们从大概上知道了 Android的体系,我强调,我们的重点是Application,包括以后的开发 我们也都是开发Application,没有其他. 我们队APP有了大概了了接,知道 .apk的组成. 那么真正的app 包含哪些细节的东西呢,都是概念性的东西,但是必须知道.所以 我们一一罗列出来,并对照关系. 首先我们讲解的是其四大组件 Acti

C# + Xamarin开发Android应用 --- Tab的实现

Layout: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layou