[英]Android change accessibility order with setAccessibilityTraversalAfter() not working
What I want is to change the accessibility order, the order in which the screen reader, the TalkBack in that Android case, read the elements, after pressing a button.我想要的是更改可访问性顺序,即屏幕阅读器、Android 案例中的 TalkBack 在按下按钮后读取元素的顺序。 This is what I tried by far, but it doesn't work, the order is still the initial descending ones.
这是我迄今为止尝试过的,但它不起作用,顺序仍然是最初的降序。 (In my example buttons are in order b1, b2, b3, and after I presso button changeorder I want the order to become b3,b1,b2).
(在我的示例中,按钮的顺序为 b1、b2、b3,在我按下按钮更改顺序后,我希望顺序变为 b3、b1、b2)。
CODE:代码:
Button b1,b2,b3, changeorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearlayout = findViewById(R.id.linearlayout);
changeorder = new Button(this);
changeorder.setText("Presso to change order");
changeorder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("BUTTON","Clicked change order button");
changeorder.setAccessibilityTraversalBefore(b3.getId());
b3.setAccessibilityTraversalAfter(changeorder.getId());
b3.setAccessibilityTraversalBefore(b1.getId());
b1.setAccessibilityTraversalAfter(b3.getId());
b1.setAccessibilityTraversalBefore(b2.getId());
b2.setAccessibilityTraversalAfter(b1.getId());
changeorder.setText("Order changed b3,b1,b2");
}
});
changeorder.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
linearlayout.addView(changeorder);
b1 = new Button(this);
b1.setText("BUTTON 1");
b1.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
linearlayout.addView(b1);
b2 = new Button(this);
b2.setText("BUTTON 2");
b2.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
linearlayout.addView(b2);
b3 = new Button(this);
b3.setText("BUTTON 3");
b3.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
linearlayout.addView(b3);
}
Your Button
s don't have IDs, so when you call getId()
in b3.setAccessibilityTraversalBefore(b1.getId());
您的
Button
没有 ID,因此当您在b3.setAccessibilityTraversalBefore(b1.getId());
调用getId()
时b3.setAccessibilityTraversalBefore(b1.getId());
, etc., you're gettingView.NO_ID
, so the traversal order isn't being set up correctly.等,您将获得
View.NO_ID
,因此未正确设置遍历顺序。
To fix it, define some IDs in XML :要修复它,请在 XML 中定义一些 ID :
<resources>
<item type="id" name="changeOrderButton" />
<item type="id" name="button1" />
<item type="id" name="button2" />
<item type="id" name="button3" />
</resources>
and then set them on your buttons with View.setId
when you create them:然后在创建它们时使用
View.setId
将它们设置在您的按钮上:
b1 = new Button(this);
b1.setId(R.id.button1); // Add the ID so that accessibility traversal can identify this view
b1.setText("BUTTON 1");
b1.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
linearlayout.addView(b1);
(and do the same for the rest of the buttons) (并对其余按钮执行相同操作)
This was too long for a comment, not an 'answer' as such.
这对于评论来说太长了,而不是像这样的“答案”。
Don't try and change the traversal order on an active element (which the button will be when you press it).不要尝试更改活动元素上的遍历顺序(按下按钮时将是该顺序)。
ie remove the changeorder.setAccessibilityTraversalBefore(b3.getId());
即删除
changeorder.setAccessibilityTraversalBefore(b3.getId());
bit.少量。
Also bear in mind that you only need to use one version of setAccessibilityTraversal
as另请记住,您只需要使用一个版本的
setAccessibilityTraversal
作为b3.setAccessibilityTraversalBefore(b1.getId());
is the same as b1.setAccessibilityTraversalAfter(b3.getId());
与
b1.setAccessibilityTraversalAfter(b3.getId());
so simplified it would be:-如此简化将是:-
public void onClick(View v) {
Log.i("BUTTON","Clicked change order button");
b3.setAccessibilityTraversalAfter(changeorder.getId());
b1.setAccessibilityTraversalAfter(b3.getId());
b2.setAccessibilityTraversalAfter(b1.getId());
changeorder.setText("Order changed b3,b1,b2");
etc.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.