Suporte ao TalkBack em apps de TV

Os apps para TV às vezes têm casos de uso que dificultam para usuários que dependem de TalkBack para usar o app. Para oferecer uma experiência melhor com o TalkBack usuários, revise cada uma das seções deste guia e implemente as alterações em seu quando necessário. Caso seu app use visualizações personalizadas, consulte também o guia correspondente que descreve como oferecer suporte à acessibilidade com recursos visualizações.

Processar visualizações aninhadas

Os usuários do TalkBack podem ter dificuldade para navegar pelas visualizações aninhadas. Sempre que possível, faça a visualização mãe ou a filha que pode ser focalizada pelo TalkBack, mas não ambas.

Para tornar uma visualização focalizável pelo TalkBack, defina focusable e as focusableInTouchMode ao true. Essa etapa é necessária porque alguns dispositivos de TV podem entrar e sair do modo de toque enquanto o TalkBack estiver ativo.

Para tornar uma visualização não focalizável, basta definir o focusable. para false. No entanto, se a visualização for acionável (ou seja, o AccessibilityNodeInfo correspondente tem ACTION_CLICK), ainda pode ser focalizável.

Mudar descrições de ações

Por padrão, o TalkBack anuncia "Pressione selecionar para ativar" para visualizações acionáveis. Para algumas ações, o termo "ativar" pode não ser uma boa descrição. Para fornecer uma descrição mais precisa, poderá alterá-la:

findViewById<View>(R.id.custom_actionable_view).accessibilityDelegate = object : View.AccessibilityDelegate() {
 
override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfo) {
   
super.onInitializeAccessibilityNodeInfo(host, info)
    info
.addAction(
     
AccessibilityAction(
       
AccessibilityAction.ACTION_CLICK.id,
        getString
(R.string.custom_label)
     
)
   
)
 
}
}
findViewById(R.id.custom_actionable_view).setAccessibilityDelegate(new AccessibilityDelegate() {
 
@Override
 
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
   
super.onInitializeAccessibilityNodeInfo(host, info);
   
AccessibilityAction action = new AccessibilityAction(
       
AccessibilityAction.ACTION_CLICK.getId(), getString(R.string.custom_label));
    info
.addAction(action);
 
}
});

Adicionar suporte a controles deslizantes

O TalkBack na TV tem suporte especial para controles deslizantes. Para ativar o modo de controle deslizante, adicione ACTION_SET_PROGRESS a uma visualização com um objeto RangeInfo.

O usuário entra no modo do controle deslizante pressionando o botão central do controle remoto da TV. Nesse modo, os botões de seta geram ACTION_SCROLL_FORWARD e Ações de acessibilidade do ACTION_SCROLL_BACKWARD.

O exemplo a seguir implementa um controle deslizante de volume com níveis de 1 a 10:

/**
 *   This example only provides slider functionality for TalkBack users. Additional logic should
 *   be added for other users. Such logic may reuse the increase and decrease methods.
 */

class VolumeSlider(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {
 
private val min = 1
 
private val max = 10
 
private var current = 5
 
private var textView: TextView? = null

 
init {
    isFocusable
= true
    isFocusableInTouchMode
= true
   
val rangeInfo =
     
AccessibilityNodeInfo.RangeInfo(
       
AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_INT,
        min
.toFloat(),
        max
.toFloat(),
        current
.toFloat()
     
)
    accessibilityDelegate
=
     
object : AccessibilityDelegate() {
       
override fun onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfo) {
         
super.onInitializeAccessibilityNodeInfo(host, info)
          info
.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SET_PROGRESS)
          info
.rangeInfo = rangeInfo
       
}

       
override fun performAccessibilityAction(host: View, action: Int, args: Bundle?): Boolean {
         
if (action == AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD.id) {
            increase
()
           
return true
         
}
         
if (action == AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD.id) {
            decrease
()
           
return true
         
}
         
return super.performAccessibilityAction(host, action, args)
       
}
     
}
 
}

 
override fun onFinishInflate() {
   
super.onFinishInflate()
    textView
= findViewById(R.id.text)
    textView
!!.text = context.getString(R.string.level, current)
 
}

 
private fun increase() {
    update
((current + 1).coerceAtMost(max))
 
}

 
private fun decrease() {
    update
((current - 1).coerceAtLeast(min))
 
}

 
private fun update(newLevel: Int) {
   
if (textView == null) {
     
return
   
}
   
val newText = context.getString(R.string.level, newLevel)
   
// Update the user interface with the new volume.
    textView
!!.text = newText
   
// Announce the new volume.
    announceForAccessibility
(newText)
    current
= newLevel
 
}
}
/**
 *   This example only provides slider functionality for TalkBack users. Additional logic should
 *   be added for other users. Such logic can reuse the increase and decrease methods.
 */

public class VolumeSlider extends LinearLayout {
 
private final int min = 1;
 
private final int max = 10;
 
private int current = 5;
 
private TextView textView;

 
public VolumeSlider(Context context, AttributeSet attrs) {
   
super(context, attrs);
    setFocusable
(true);
    setFocusableInTouchMode
(true);
   
RangeInfo rangeInfo = new RangeInfo(RangeInfo.RANGE_TYPE_INT, min, max, current);
    setAccessibilityDelegate
(
       
new AccessibilityDelegate() {
         
@Override
         
public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
           
super.onInitializeAccessibilityNodeInfo(host, info);
            info
.addAction(AccessibilityAction.ACTION_SET_PROGRESS);
            info
.setRangeInfo(rangeInfo);
         
}

         
@Override
         
public boolean performAccessibilityAction(View host, int action, Bundle args) {
           
if (action == AccessibilityAction.ACTION_SCROLL_FORWARD.getId()) {
              increase
();
             
return true;
           
}
           
if (action == AccessibilityAction.ACTION_SCROLL_BACKWARD.getId()) {
              decrease
();
             
return true;
           
}
           
return super.performAccessibilityAction(host, action, args);
         
}
       
});
 
}

 
@Override
 
protected void onFinishInflate() {
   
super.onFinishInflate();
    textView
= findViewById(R.id.text);
    textView
.setText(getContext().getString(R.string.level, current));
 
}

 
private void increase() {
    update
(Math.min(current + 1, max));
 
}

 
private void decrease() {
    update
(Math.max(current - 1, min));
 
}

 
private void update(int newLevel) {
   
if (textView == null) {
     
return;
   
}
   
String newText = getContext().getString(R.string.level, newLevel);
   
// Update the user interface with the new volume.
    textView
.setText(newText);
   
// Announce the new volume.
    announceForAccessibility
(newText);
    current
= newLevel;
 
}
}