Android - prevent TalkBack from announcing TextVie

2020-02-01 07:15发布

I am developing an accessible android application where people would be using Explore by Touch and TalkBack accessibility services to use my application.

This is my Android XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/forename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:layout_marginLeft="15dip"
        android:textSize="20sp"
        android:text="@string/forenameText" 
        android:contentDescription="@null"/>

    <EditText
        android:id="@+id/EditTextForename"
        android:layout_width="285dp"
        android:layout_height="65dp"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="15dip"
        android:hint="@string/forenameHint"
        android:inputType="textPersonName"
        android:lines="1"
        android:singleLine="true"
        android:textSize="20sp" >
    </EditText>

</LinearLayout>

strings.xml

<string name="forenameText">Forename</string>
<string name="forenameHint">Enter your forename here</string>

TextView displays the title "Forename" and EditText allows me to enter some details in the form field. The problem I have is that when I drag my finger across the screen by using Explore by Touch, TalkBack picks up the title of the TextView and announces it aloud as "Forename". I want the TextView to only display text and not provide any audible feedback.

I have set contentDescription to @null as you can see from the code above, but TalkBack still announces "Forename" when my finger is located over the TextView.

I have also tried setting contentDescription in my Java class:

TextView forename=(TextView)findViewById(R.id.forename);
forename.setContentDescription("");

However, I still get the same problem. Is there any other way to set contentDescription to null/empty and prevent TalkBack from announcing it aloud?

Java code:

public class MainActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        View forename = findViewById(R.id.forename);

        forename.setAccessibilityDelegate(new AccessibilityDelegate() {
          public boolean performAccessibilityAction (View host, int action, Bundle args){
            return true;
          }
        });
    }

}

6条回答
太酷不给撩
2楼-- · 2020-02-01 07:39

I was trying to do the same today, and was able to set an 'empty' contentDescription on a TextView like so (using a non-breaking whitespace):

decorativeTextView.setContentDescription("\u00A0"); 

now TalkBack doesn't say anything for that TextView.

but I agree with Nick about leaving the label as readable in your case, because hint is only read for empty EditTexts.

查看更多
贼婆χ
3楼-- · 2020-02-01 07:40

I had a similar problem. I eventually solved it by using the setAccessibilityDelegate method and overriding View.AccessibilityDelegate's performAccessibilityAction method.

try this:

View forename = findViewById(R.id.forename);

forename.setAccessibilityDelegate(new AccessibilityDelegate() {
  public boolean performAccessibilityAction (View host, int action, Bundle args){
    return true;
  }
});
查看更多
做个烂人
4楼-- · 2020-02-01 07:43

Since API 16, Android introduced the following:

android:importantForAccessibility="no"

or

setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO)

Which allows developers to disable talkback all together for certain views.

http://developer.android.com/reference/android/view/View.html

查看更多
Deceive 欺骗
5楼-- · 2020-02-01 07:47

I had the same problem, and the only thing that worked for me was android:contentDescription=" " (white space).

查看更多
够拽才男人
6楼-- · 2020-02-01 07:48

Why do you not want the TextView to speak "forename"? It is being used as a label for the EditText. Once the user has entered some text the hint "enter your forename here" would no longer be spoken - as far as I know - so the TextView given the user some context for the EditText.

Similarly the announcement of "editbox" gives the user the role of the EditText control. While "form field" might be better it would not be the same behavior as in other apps and in the OS.

查看更多
何必那么认真
7楼-- · 2020-02-01 07:55

For better backwards compatibility:

ViewCompat.setImportantForAccessibility(
        decorativeTextView,
        ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_NO);
查看更多
登录 后发表回答