【问题标题】:unable to send accessibility event in Android无法在 Android 中发送可访问性事件
【发布时间】:2015-10-28 17:31:17
【问题描述】:

我正在尝试在显示快餐栏后立即发送辅助功能。但是什么都没有播放。这是我在 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);
       }

    }

【问题讨论】:

    标签: android android-snackbar accessibility


    【解决方案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);}
    

    你可以看到here

    【讨论】:

      【解决方案2】:

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

      @Override 
      public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
        super.onInitializeAccessibilityEvent(event);
        event.getText().add("some more to say");
      } 
      

      【讨论】:

      • 不,因为我希望它在显示小吃店后立即被解雇。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多