2015-11-02 16 views
0

我是新的開發android應用程序使用Xamarin。我正在開發一個包含許多對話框和活動的應用程序,其中一個對話框具有這樣的佈局,一個時間選擇器,兩個標記爲「取消」和「確定」的按鈕以及一個文本視圖。我想做這樣的事情,當用戶點擊「確定」按鈕時,timepicker中選擇的時間將顯示在textview上。 (或者我想提取按鈕點擊事件中的timepicker的值)需要幫助xamarin.android與時間選擇器

+0

在SO,您必須證明您已經嘗試了什麼然後再問「如何去做這件事」。 –

回答

2

這很簡單。檢查了這一點:

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

     } 
+0

感謝它的工作:) – Dev

+1

@KunalVerma歡迎您。 你也可以標記爲解決方案我的回答:) – XTL