dispatchPopulateAccessibilityEvent不会触发dispatchPopulateAccessibilityEvent

2023-09-12 05:49:02 作者:╯幸福°持有者

我想在我的应用程序来实现辅助功能。我很困惑,它实际上是如何工作的。我从我的事件发送不同的包中一个简单的ImageView为:

I'm trying to implement accessibility feature in my app. I'm confused about how it actually works. I've a simple imageView in different package from where I send events as:

public final void onClick(final android.view.View v) {
            android.util.Log.v(CLASSNAME,"onClick tag:"+v.getTag());
            v.sendAccessibilityEvent(android.view.accessibility.AccessibilityEvent.TYPE_VIEW_CLICKED);

我已经implmented辅助类,如下所示:

I've implmented Accessibility class as follows:

import android.content.Context;

public final class Accessibility extends android.view.View {
     public Accessibility(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }



    public static android.view.accessibility.AccessibilityManager manager = (android.view.accessibility.AccessibilityManager)getSystemService(android.content.Context.ACCESSIBILITY_SERVICE); 
    public static android.view.accessibility.AccessibilityEvent event = android.view.accessibility.AccessibilityEvent.obtain();

    public static Accessibility accessibility;


    public static boolean isEenabled()
    {

    if(manager.isEnabled())
        return true;
    else
        return false;
    }


    @Override
    public boolean dispatchPopulateAccessibilityEvent(android.view.accessibility.AccessibilityEvent event)
    {
        super.dispatchPopulateAccessibilityEvent(event);
        android.util.Log.e(CLASSNAME, "Came here");
                        {

            event.getText().add("Click here!!");
            event.setEnabled(true);
                            return true;
        }

我读了开发文档,我必须重写视图类中的方法(这是我延长我的课查看的原因)。我意识到,这种方法也存在于活动类(即扩展活动)调用该方法,甚至没有发送任何事件。

I read the Developer docs and I have to override the method in View class (that's the reason I extended my class to View). I realized this method also exists in Activity class (i.e. extends Activity) which calls this method without even sending any events.

我知道我在做一些愚蠢的,但无法找到确切位置。

I know I'm doing something silly, but not able to find where exactly.

PS:我没设置一个断点v.sendAccessibilityEvent - 它道出View.java并以某种方式失败的条件!AccessibilityManager.getInstance(mContext)= NULL

P.S.: I did set a breakpoint on v.sendAccessibilityEvent - It goes to View.java and somehow it fails at condition "AccessibilityManager.getInstance(mContext)!= null"

推荐答案

那不叫的原因是因为系统调用你的ImageView的dispatchPopulateAccessibilityEvent(因为你从那里发送的情况下),而不是你的辅助功能对象之一。

The reason it is not called is because the system calls dispatchPopulateAccessibilityEvent of your ImageView(because you send the event from there), not the one of your Accessibility object.

要实现自定义的访问行为,你有3个选项:

To implement custom accessibility behavior you have 3 options:

实现自定义视图扩展ImageView的(并覆盖dispatchPopulateAccessibilityEvent方法和其他辅助功能)。这是一个更通用的方法,让开发人员模拟系统部件可访问性的行为。

Implement a custom View that extends ImageView(and override the dispatchPopulateAccessibilityEvent method and other accessibility). This is a more generic approach and lets the developer imitate system widgets accessibility behavior.

下面是添加自定义文本对 TYPE_VIEW_CLICKED 事件的事件对象的示例类。您需要设置它的点击属性为true,以便它可以接收点击的事件。我已经重写 onPopulateAccessibilityEvent ,而不是 dispatchPopulateAccessibilityEvent ,因为这个类没有孩子,没有必要覆盖调度算法

Here's an example class that adds custom text to the event object on TYPE_VIEW_CLICKED event. You need to set its clickable attribute to true so it can receive clicked events. I've overridden onPopulateAccessibilityEvent rather than dispatchPopulateAccessibilityEvent because this class doesn't have children and there's no need to override dispatching algorithm.

public class CustomImageView  extends ImageView {

    private static final String TAG = CustomImageView.class.getSimpleName();

    /* Here are constructors from ImageView */

    @Override
    public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(event);

        if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED) {
            Log.v(TAG, "Populating accessibility event");
            event.getText().add("Custom text from onPopulateAccessibilityEvent");
        }
    }
}

实施 AccessibilityDelegate 。这不是作为一般作为第一种方法,但对于简单的应用更容易,并允许更多复杂的辅助行为

Implement AccessibilityDelegate. This is not as generic as the first method, but for simple applications it's easier and allows more complex accessibility behavior.

下面是做类似的事情的CustomImageView的AccessibilityDelegate。其主要优点是,你可以重复使用相同的AccessibilityDelegate针对不同的对象和类。

Here's an AccessibilityDelegate that does similar thing to the CustomImageView. The main advantage is that you can reuse same AccessibilityDelegate for different objects and classes.

imageView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
    @Override
    public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
        super.onPopulateAccessibilityEvent(host, event);

        if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED) {
            Log.v(TAG, "Populating accessibility event");
            event.getText().add("Custom text from AccessibilityDelegate");
        }
    }
});

结合这两种方法,如果你需要一个非常复杂的可访问性的逻辑。

Combine both methods if you need a very complex accessibility logic.

有在查看多种使用方式,阅读文档,仔细哪些是你真正需要重写。

There are many accessibility methods in View, read documentation carefully which ones you really need to override.

相关推荐
 
精彩推荐
图片推荐