【问题标题】:Xamarin Android Accessibility - how can we set accessibility focus to a UI element like a button programaticallyXamarin Android Accessibility - 我们如何以编程方式将可访问性焦点设置为 UI 元素(如按钮)
【发布时间】:2017-01-03 15:18:25
【问题描述】:

在我的 Xamarin Android 应用程序中,用户需要从微调器(下拉菜单)中选择一个项目。从微调器中选择任何项目后,可访问性焦点将转移到屏幕的开头而不是下一个可聚焦元素,这不是预期的行为。 因此试图找到一种以编程方式将焦点设置到按钮的方法,但在 Xamarin Android 中似乎没有办法。

我试过了

Button1.RequestFocus();

Button1.RequestFocusFromTouch();

Button1.SendAccessibilityEvent(EventTypes.ViewAccessibilityFocused);

但没有任何效果!

以下代码示例与Xamarin iOS相关:

    UIAccessibility.PostNotification(UIAccessibilityPostNotification.ScreenChanged, myFirstElement);

这种在 Xamarin iOS 中可用的方法运行良好,因此 Xamarin Android 需要类似的方法以编程方式聚焦。

在这里,我发布示例代码。

我的 .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">
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"   android:focusable="true"    android:focusableInTouchMode="true"/>
    <Button
        android:text="Add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnAdd" 
        android:focusable="true"
        android:focusableInTouchMode="true"/>
</LinearLayout> </LinearLayout>

我的活动文件

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        this.SetContentView (Resource.Layout.Main);
Spinner spinner = this.FindViewById<Spinner>(Resource.Id.spinner);
        Button btnAdd = this.FindViewById<Button>(Resource.Id.btnAdd);
var adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.planets_array, Android.Resource.Layout.SimpleSpinnerItem);

            adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinner.Adapter = adapter;
            btnAdd.Click += (s, e) =>
              {
                  string toasts = string.Format("Add button got focus");
                  Toast.MakeText(this, toasts, ToastLength.Long).Show();
              };

        }

        private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
        {
            Spinner spinner = (Spinner)sender;

            string toast = string.Format("The planet is {0}", spinner.GetItemAtPosition(e.Position));
            Toast.MakeText(this, toast, ToastLength.Long).Show();
            //btnAdd.RequestFocus();
            btnAdd.RequestFocusFromTouch();
            btnAdd.RequestFocus();
            if (btnAdd.IsFocused)
            {
                string toasts = string.Format("Add button got focus");
                Toast.MakeText(this, toasts, ToastLength.Long).Show();                
            }
            skillSeleted = "selected";
        }

        protected override void OnResume()
        {
            base.OnResume();
            if (skillSeleted == "selected")
            {
                btnAdd.RequestFocusFromTouch();
                btnAdd.RequestFocus();
            }
        }


//Add this to String file under values folder:
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>

请在 Samsung Mobile 中使用 Marshmallow android 版本查看此场景。

提前致谢。

【问题讨论】:

  • 对于普通的按钮设置请求焦点方法有效,但在这种情况下,从微调器中选择一个项目后,焦点会以某种方式丢失,因此焦点会转到页面的开头。请求关注项目选定事件也不起作用。
  • 你在设置focusableInTouchMode吗?
  • 是的,我设置了 'Focusable=true' 和 'FocusableInTouchMode=true'
  • 你可能不得不发布一些可重现的 AXML 和代码

标签: android xamarin focus spinner accessibility


【解决方案1】:
public class MainPage : ContentPage {
    public MainPage() => Content = BuildView().CreateView();

    void SetState() => BuildView().Apply(Content);

    int _counter;

    Command _push => new Command(() => {
        _counter++;
        SetState();
    });

    ViewModel BuildView() =>
        new StackLayoutModel(
            children: new ViewModel[] {
                new LabelModel(text: _counter.ToString(), fontSize: 42),
                new ButtonModel(text: "Increment", command: _push),
            },
            padding: new Thickness(42));
}

【讨论】:

    猜你喜欢
    • 2023-03-02
    • 1970-01-01
    • 2015-12-30
    • 2011-03-15
    • 1970-01-01
    • 2019-12-07
    • 2014-03-06
    • 2019-05-04
    • 2021-10-30
    相关资源
    最近更新 更多