gpt4 book ai didi

c# - 日期选择器 : How to popup datepicker when click on edittext using C# xamarin

转载 作者:太空狗 更新时间:2023-10-29 22:07:59 26 4
gpt4 key购买 nike

如何在 Xamarin.Android 中的编辑文本点击或焦点事件上显示日期选择器弹出窗口?

最佳答案

为 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();
}
}
}

关于c# - 日期选择器 : How to popup datepicker when click on edittext using C# xamarin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37477860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com