【问题标题】:how to select date in date picker using Keyboard alone in xamarin android application如何在 xamarin android 应用程序中单独使用键盘在日期选择器中选择日期
【发布时间】:2023-03-10 00:40:01
【问题描述】:
我正在开发一个 xamarin android 应用程序,我在其中使用日期选择器来选择出生日期。我只想通过使用键盘导航来选择日期。我可以使用制表符和箭头键浏览月份和年份,但无法使用键盘选择日期。
我使用“Theme.AppCompat.Light.DarkActionBar”作为主题父级。
希望,任何人都可以帮助我。
【问题讨论】:
标签:
android
xamarin
datepicker
keyboard
accessibility
【解决方案1】:
我只想使用键盘导航来选择日期。我可以使用制表符和箭头键浏览月份和年份,但无法使用键盘选择日期。
您可以使用 Activity 的 OnkeyUp 事件来实现:
public class MainActivity : Activity
{
DatePicker mDatePicker;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
mDatePicker = FindViewById<DatePicker>(Resource.Id.mDatePicker);
}
public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
//when user press c, the date of the DatePicker will be updated
if (keyCode == Keycode.C)
{
mDatePicker.UpdateDate(mDatePicker.Year, mDatePicker.Month, mDatePicker.DayOfMonth + 1);
}
return true;
}
}
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="match_parent"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/mDatePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>