如何在 Xamarin 中指示当前选定的控件? [英] How to indicate currently selected control in Xamarin?

查看:25
本文介绍了如何在 Xamarin 中指示当前选定的控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 Android 设备没有任何触摸屏.它只有一个可以生成 KEYCODE_DPAD_UP、KEYCODE_DPAD_DOWN 等的触摸板.但是,我不确定我的 Xamarin 应用程序是否响应这些事件.是否可以启用任何设置来识别一组控件中的当前选择.

The Android device I am using does not have any touchscreen. All it has is a touch pad that can generate KEYCODE_DPAD_UP, KEYCODE_DPAD_DOWN, etc. However, I am not sure if my Xamarin app is responding to these events. Is there any setting that one can enable to identify current selection among a group of controls.

当我进入设备的主屏幕并使用 DPad 键四处移动时,我看到当前选定的图标周围有一个细小的正方形.我希望 Xamarin 应用程序中有一个类似的设置来打开这种行为.问候.

When I go the device's home screen and use the DPad keys to move around, I see a thin square around the icon that is currently selected. I am hoping there is a similar setting within Xamarin app to turn on this behavior. Regards.

推荐答案

您可以简单地使用活动的 CurrentFocus 来获取当前的焦点控件.但首先,当控件获得焦点时,它会被高亮,通常我们可以将控件的背景修改为可绘制状态列表资源例如:

You can simply use CurrentFocus of the activity to get the current focused control. But first of all, when control is focused, it will be highlighted, usually we can modify the control's background to a drawable state list resource for example:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

此视觉行为用于指示用户已导航到控件的按钮.

This visual behavior is for a button to indicate that user has navigated to the control.

当我进入设备的主屏幕并使用 DPad 键四处移动时,我看到当前选定的图标周围有一个细小的正方形.我希望 Xamarin 应用中有一个类似的设置来开启这种行为.

When I go the device's home screen and use the DPad keys to move around, I see a thin square around the icon that is currently selected. I am hoping there is a similar setting within Xamarin app to turn on this behavior.

所以这个细小方块可以作为聚焦状态的设计背景.

So this thin square can be the designed background for focused state.

不知道有没有什么设置,但是如果你想在后面的代码中做来指示当前关注哪个控件,正如我开头所说的,你可以使用CurrentFocus.例如:

I don't know if there is any setting for it, but if you want to do it in code behind to indicate which control is currently focused, as I said at the beginning, you can use CurrentFocus of the activity. For example:

public override bool DispatchKeyEvent(KeyEvent e)
{
    if (e.Action == KeyEventActions.Up)
    {
        if (e.KeyCode == Keycode.DpadDown || e.KeyCode == Keycode.DpadUp
            || e.KeyCode == Keycode.DpadLeft || e.KeyCode == Keycode.DpadRight)
        {
            var view = this.CurrentFocus;
            view.SetBackgroundDrawable(...);
        }
    }
    return base.DispatchKeyEvent(e);
}

忘记说了,如果你想在代码后面做,不要忘记在新控件获得焦点时将背景改回原来的状态.emmm,看来状态列表资源是这里的最佳选择.

Forget to say, if you want to do it in code behind, don't forget to change the background back to its old state when a new control is focused. Emmm, seems the state list resource is the best choice here.

这篇关于如何在 Xamarin 中指示当前选定的控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆