活动中的时间和日期选择器对话框

时间:2015-11-23 07:53:01

标签: c# android-activity xamarin

我正在使用Xamarin Studio开发移动应用程序。我想在活动中添加时间和日期选择器。我试过在一个片段上做它并且效果很好。但现在我想在一项活动中这样做。

1 个答案:

答案 0 :(得分:0)

我真的不明白这个

  

我已尝试在片段上执行此操作并且效果很好

因为它一样:)!

好的,请检查 out (通过此示例,您可以将日期和时间选择器中的值提取到textview中):

您的Main.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">
    <TimePicker
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/picker" />
    <Button
        android:text="OK"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnOK" />
    <Button
        android:text="Cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnCancel" />
    <TextView
        android:text="Text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt"
        android:textSize="40dp" />
</LinearLayout>  

并且您的 OnCreate()方法必须包含以下内容:

  protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            Button OK = FindViewById<Button>(Resource.Id.btnOK);
            Button Cancel = FindViewById<Button>(Resource.Id.btnCancel);
            TimePicker picker = FindViewById<TimePicker>(Resource.Id.picker);
            TextView txt = FindViewById<TextView>(Resource.Id.txt);

            OK.Click += (sender, e) => 
                {
                    //getting values from TImePicker via CurrentHour/CurrentMinutes
                    txt.Text = String.Format("{0} : {1}",picker.CurrentHour,picker.CurrentMinute);
                };

        }
相关问题