rat*_*ain 5 c# android-datepicker xamarin.android xamarin
如何在Xamarin.Android中的编辑文本点击或焦点事件中显示日期选择器弹出窗口?
Vai*_*esh 12
尝试使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
尝试使用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>
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于那些喜欢MVVM的人。这是Xamarin Forms中的MVVM实现。我在这里使用事件命令行为。您可以从此链接Xamarin Event到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>
Run Code Online (Sandbox Code Playgroud)
查看型号代码
OnSelectDOBCommand = new RelayCommand<DatePicker>(FocusDatePicker);
DateSelectedCommand = new RelayCommand(SetDateOfBirth);
Run Code Online (Sandbox Code Playgroud)
现在,函数定义
private void FocusDatePicker(DatePicker obj)
{
obj.Focus();
}
private void SetDateOfBirth()
{
DateOfBirth = SelectedDOB;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助某人
更新11/11/2016
另一种选择是使用ACR.Userdialogs简单明了。声明您的ICommand
OnSelectDOBCommand = new RelayCommand(ShowDatePicker);
Run Code Online (Sandbox Code Playgroud)
接下来定义函数ShowDatePicker
private void ShowDatePicker()
{
UserDialogs.Instance.DatePrompt(new DatePromptConfig { MaximumDate = MaxDate, OnAction = (result) => SetDateOfBirth(result), IsCancellable = true });
}
Run Code Online (Sandbox Code Playgroud)
并设定生日功能
private void SetDateOfBirth(DatePromptResult result)
{
if(result.Ok)
{
DateOfBirth = result.SelectedDate.ToString("dd MMM yyyy");
}
}
Run Code Online (Sandbox Code Playgroud)
最好的部分是,您根本不需要任何特殊的Xaml。您可以将其挂接到任何按钮。
应该给艾伦一个奖项或其他