3

我正在尝试在显示小吃店后立即发送可访问性。但是什么都没有播放。这是我在 onStart() 生命周期方法中的代码:

            final Snackbar snackbar = Snackbar
                       .make(findViewById(R.id.container), "snackbars are cool", Snackbar.LENGTH_INDEFINITE);
               snackbar.setActionTextColor(getResources().getColor(R.color.snackbar_actions));


        View snackbarView = snackbar.getView();
   snackbarView.setBackgroundColor(Color.DKGRAY);
   TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
   textView.setTextColor(Color.WHITE);
   snackbar.show();


//time to send a accessibility event below
       final ViewGroup parentView = (ViewGroup)findViewById(R.id.container);
       if (parentView != null) {
           final AccessibilityManager a11yManager =
                   (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);

           if (a11yManager != null && a11yManager.isEnabled()) {
               final AccessibilityEvent e = AccessibilityEvent.obtain();
               snackbarView.onInitializeAccessibilityEvent(e);
               e.getText().add("some more text to say");
//nothing happens here

           parentView.requestSendAccessibilityEvent(snackbarView, e);
       }

    }
4

2 回答 2

1

一些开发人员为此编写了修复程序。我的另一个问题是由于某种原因我不应该在 onStart 中调用它。无论如何,我创建了一个 utils 方法并使用了如下所示的开发人员命令:

if (!mA11yManager.isEnabled()) {
    return;
}

// Prior to SDK 16, announcements could only be made through FOCUSED
// events. Jelly Bean (SDK 16) added support for speaking text verbatim
// using the ANNOUNCEMENT event type.
final int eventType;
if (Build.VERSION.SDK_INT < 16) {
    eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED;
} else {
    eventType = AccessibilityEventCompat.TYPE_ANNOUNCEMENT;
}

// Construct an accessibility event with the minimum recommended
// attributes. An event without a class name or package may be dropped.
final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
event.getText().add(text);
event.setEnabled(isEnabled());
event.setClassName(getClass().getName());
event.setPackageName(mContext.getPackageName());

// JellyBean MR1 requires a source view to set the window ID.
final AccessibilityRecordCompat record = new AccessibilityRecordCompat(event);
record.setSource(this);

// Sends the event directly through the accessibility manager. If your
// application only targets SDK 14+, you should just call
// getParent().requestSendAccessibilityEvent(this, event);
mA11yManager.sendAccessibilityEvent(event);}

你可以在这里看到

于 2015-10-28T19:56:27.540 回答
0

如果您想为您的可访问性事件添加更多信息,您可以AccessibilityDelegate为您的视图实现并覆盖onInitializeAccessibilityEvent

@Override 
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
  super.onInitializeAccessibilityEvent(event);
  event.getText().add("some more to say");
} 
于 2015-10-28T18:47:19.273 回答