簡體   English   中英

從 Xamarin Android 中的 TimePicker 獲取選定時間

[英]Get Selected Time from TimePicker in Xamarin Android

因為他們說 Java 可以做的任何事情,C# 可以做得更好......決定在 Xamarin Android 中設計一個警報應用程序,我似乎無法在 C# 中獲得所選時間的值......一個稱為 triggerTime 的變量應該播放一些音樂或在鬧鍾時間成熟時顯示警報對話框需要該值...這是 TimePicker 的 xml...

 <TimePicker
        android:id="@+id/timePicker1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/>

這是在 AlarmActivity.cs 中定義了時間選擇器的 C# 代碼

 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;

        };

截屏:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM