如何解决从Xamarin Android中的TimePicker获取选定的时间
由于他们说了Java可以做的任何事情,所以C#可以做得更好...决定在Xamarin Android中设计一个警报应用程序,而我似乎无法在C#中获得所选时间的值...一个称为TriggerTime的变量当警报时间到期时,应该播放一些音乐或显示警报对话框需要该值... 这是TimePicker的xml ...
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
这是C#代码,具有在AlarmActivity.cs中定义的时间选择器
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this,savedInstanceState);
SetContentView(Resource.Layout.Alarm);
TimePicker timePicker2 = this.FindViewById<TimePicker>(Resource.Id.timePicker1);
//Code to getTime that user selects
感谢您的帮助
解决方法
Xamarin提供了使用TimePicker
的完整示例基本上,您需要实现具有IOnTimeSetListener
方法的OnTimeSet
public void OnTimeSet(TimePicker view,int hourOfDay,int minute)
{
// hourOfDate and minute are the user's selected values
}
,
您可以使用TimeChanged
事件获取时间选择器更改的时间。
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/timePicker1"/>
</RelativeLayout>
背后的代码:
TextView textView = FindViewById<TextView>(Resource.Id.textView1);
TimePicker timePicker1 = FindViewById<TimePicker>(Resource.Id.timePicker1);
timePicker1.TimeChanged += delegate
{
var h = timePicker1.CurrentHour;
var m = timePicker1.CurrentMinute;
textView.Text = h + ":" + m;
};
截屏:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。