使用C# + Xamarin开发Android 应用 -- Datetime Picker
2015-06-01 10:04
447 查看
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;
}
}
<?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;
}
}
相关文章推荐
- android广播注册简单用法
- Android中动态更新ListView
- android学到啥时候就高级了啊
- android编码相关的理解
- Android EditText 不弹出软件键盘
- Android 仿 窗帘效果 和 登录界面拖动效果 (Scroller类的应用) 附 2个DEMO及源码
- Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)
- Android 实现ActionBar定制
- Android性能优化之查看GPU渲染性能
- Android 3.0之后开机无法接收系统广播权限原因
- Android性能优化之Overdraw
- android:layout_weight属性详解 (转)
- Android平台根目录文件
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
- Android开发相关的Blog推荐——跟随大神的脚步才能成长为大神
- Android Button响应点击事件的几种方法大合集
- xbmc 从android_main启动时的部分步骤记录
- android GreenDao数据库框架学习(1)
- Android下利用Fragment+RadioGroup和TabHost实现底部选项卡的效果
- Android知识点