2016-05-27 49 views

回答

8

嘗試這樣的Xamarin.Forms:

public class SamplePage : ContentPage 
{ 
    public SamplePage() 
    { 
     var editText = new Entry { 
      Placeholder = "Select Date.", 
     }; 

     var date = new DatePicker { 
      IsVisible = false, 
      IsEnabled = false, 
     }; 

     var stack = new StackLayout { 
      Orientation = StackOrientation.Vertical, 
      Children = {editText, date} 
     }; 

     editText.Focused += (sender, e) => { 
      date.Focus(); 
     }; 
     date.DateSelected += (sender, e) => { 
      editText.Text = date.Date.ToString(); 
     }; 

     Content = stack; 
    } 
} 


編輯:

嘗試像這樣的Xamarin.Android:

個MyLayout.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"> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" /> 
</LinearLayout> 

MyLayoutActivity.cs

using System; 
using Android.App; 
using Android.OS; 
using Android.Widget; 

namespace AndiNative 
{ 
    [Activity (Label = "MyLayoutActivity", MainLauncher = true)]    
    public class MyLayoutActivity: Activity 
    { 
     private static EditText editText; 

     protected override void OnCreate (Bundle bundle) 
     { 
      base.OnCreate (bundle); 
      SetContentView (Resource.Layout.DatePickerTest); 

      editText = FindViewById<EditText> (Resource.Id.editText); 

      editText.Click += (sender, e) => { 
       DateTime today = DateTime.Today; 
       DatePickerDialog dialog = new DatePickerDialog(this, OnDateSet, today.Year, today.Month - 1, today.Day); 
       dialog.DatePicker.MinDate = today.Millisecond; 
       dialog.Show(); 
      }; 
     } 

     void OnDateSet(object sender, DatePickerDialog.DateSetEventArgs e) 
     { 
      editText.Text = e.Date.ToLongDateString(); 
     } 
    } 
} 
+0

vaikesh會有點特殊.. –

+0

我要創建一個類..有關click事件的代碼 –

+0

@ratnamanjariswain什麼:檢查代碼,我已經更新了。 – Vaikesh

2

對於那些你們誰喜歡MVVM。這是Xamarin Forms中的MVVM實現。我在這裏使用了Event to Command行爲。你可以得到更多,從這個鏈接 Xamarin Event to Command Behaviors

的XAML

<Entry Text="{Binding DateOfBirth, Mode=TwoWay}" /> 
    <Image Source="button.png" WidthRequest="39"> 
     <Image.GestureRecognizers> 
      <TapGestureRecognizer Command="{Binding OnSelectDOBCommand}" CommandParameter="{Binding ., Source={Reference DOBPicker}}" /> 
     </Image.GestureRecognizers> 
    </Image> 
    <DatePicker x:Name="DOBPicker" 
       Date="{Binding SelectedDOB}" 
       IsEnabled="True" 
       IsVisible="False"> 
     <DatePicker.Behaviors> 
      <behaviors:DatePickerSelectedBehavior Command="{Binding DateSelectedCommand}" EventName="DateSelected" /> 
     </DatePicker.Behaviors> 
    </DatePicker> 

查看型號代碼

OnSelectDOBCommand = new RelayCommand<DatePicker>(FocusDatePicker); 
DateSelectedCommand = new RelayCommand(SetDateOfBirth); 

現在函數定義

private void FocusDatePicker(DatePicker obj) 
    { 
     obj.Focus(); 
    } 
    private void SetDateOfBirth() 
    { 
     DateOfBirth = SelectedDOB; 
    } 

希望這可以幫助別人

更新2016年11月11日

的另一種方法是使用ACR.Userdialogs簡單明瞭。聲明你的ICommand

OnSelectDOBCommand = new RelayCommand(ShowDatePicker); 

下一頁定義函數ShowDatePicker

private void ShowDatePicker() 
{    
    UserDialogs.Instance.DatePrompt(new DatePromptConfig { MaximumDate = MaxDate, OnAction = (result) => SetDateOfBirth(result), IsCancellable = true }); 
} 

和生育功能

private void SetDateOfBirth(DatePromptResult result) 
    {    
     if(result.Ok) 
     { 
      DateOfBirth = result.SelectedDate.ToString("dd MMM yyyy"); 
     } 

    } 

最好的部分的設定日期,你不需要任何特殊的XAML的。您可以將其掛接到任何按鈕。

艾倫應給予獎勵或東西

1
<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginBottom="15dp" 
    android:id="@+id/datePickerText" 
    android:textColor="@color/textColor" 
    android:textColorHint="@color/textColor" 
    android:textSize="14dp" 
    local:MvxBind="Value Format('{0:dd MMM yyyy}',Date)" /> 

財產viewmodel-

private DateTime _date = DateTime.Today; 
    public DateTime Date 
    { 
     get 
     { 
      return _date; 
     } 
     set 
     { 
      _date = value; 
      RaisePropertyChanged(() => Date); 
     } 
    } 

在我的講座

 private EditText datePickerText; 
     private DatePickerDialogFragment dialog; 
     protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.report); 

     var dp = new MvxDatePicker(this); 
     dp.SpinnersShown = true; 
     dp.DateTime = DateTime.Now; 
     datePickerText.Focusable = false; 
     datePickerText.Text = ViewModel.Date.ToString("dd-MMM-yyyy"); 

     datePickerText.Click += (sender, args) => 
     { 
     if(datePickerText.Text == "") 
    dialog = new DatePickerDialogFragment(this, DateTime.Now,this); 
     else 
    dialog = new DatePickerDialogFragment(this,Convert.ToDateTime(datePickerText.Text), this); 

    dialog.Show(this.SupportFragmentManager, "date"); 
     }; 

     datePickerText.TextChanged += (sender, args) => 
     { 
      ViewModel.Date = Convert.ToDateTime(datePickerText.Text); 
     }; 
    } 

    public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
    { 
     datePickerText.Text = new DateTime(year, monthOfYear + 1, dayOfMonth).ToString("dd-MMM-yyyy"); 
    } 

這裏所花費的默認日期對於今天的情況而言,它會改變日期由用戶選擇

+0

@ratnamanjariswain - 接受我的答案,如果它幫助你。你問這個問題的xamarin.android使用MVVM所以該解決方案將幫助你!最好的祝福.. :) – Ruchira

相關問題