2015-10-28 65 views
1

我試圖在顯示小吃店後發送輔助功能。但沒有什麼可玩的。這裏是我的代碼是在調用onStart()的生命週期方法:無法在Android中發送輔助功能事件

  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); 
     } 

    } 

回答

0

如果您想更多信息添加到您的可訪問性事件,你可以實現AccessibilityDelegate對你的看法,並覆蓋onInitializeAccessibilityEvent

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

沒有,因爲我希望它得到正確的儘快解僱作爲正在顯示的小吃吧是不。 – j2emanue

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