【问题标题】:Need help in xamarin.android with timepicker使用 timepicker 在 xamarin.android 中需要帮助
【发布时间】:2015-11-03 20:39:26
【问题描述】:

我是使用 Xamarin 开发 android 应用程序的新手。我正在开发一个包含许多对话框和活动的应用程序,其中一个对话框具有这样的布局、一个时间选择器、两个标记为“取消”和“确定”的按钮以及一个文本视图。我想做这样的事情,当用户单击“确定”按钮时,时间选择器中选择的时间将显示在 textview 上。 (或者我想在按钮点击事件中提取时间选择器的值)

【问题讨论】:

  • 在 SO,您必须先展示您尝试过的内容,然后再询问“how-to-go-about-this”。

标签: android xamarin


【解决方案1】:

这很简单。看看这个:

你的 Main.axml(layout)

<?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);
                };

        }

【讨论】:

  • @KunalVerma 不客气。您也可以将我的答案标记为解决方案:)
猜你喜欢
  • 2012-05-27
  • 2016-07-29
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多