gpt4 book ai didi

android - 填充 TextView 列表时动态设置 contentDescription

转载 作者:行者123 更新时间:2023-11-29 14:51:59 26 4
gpt4 key购买 nike

我正在尝试更新 Android 的示例代码“API 演示”应用程序,以便默认 Activity 列表中的 TextView 项目具有可访问性所需的“内容描述”。这是此 Activity 中列表的初始化情况:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = getIntent();
String path = intent.getStringExtra("com.example.android.apis.Path");

if (path == null) {
path = "";
}

setListAdapter(new SimpleAdapter(this, getData(path),
android.R.layout.simple_list_item_1, new String[] { "title" },
new int[] { android.R.id.text1 }));
getListView().setTextFilterEnabled(true);
}

getData 函数创建了一个 map ,其中指定了当有人点击 TextView 列表元素之一时指定的适当 Intent 。在内部,它使用这个函数:

protected void addItem(List<Map<String, Object>> data, String name, Intent intent) {
Map<String, Object> temp = new HashMap<String, Object>();
temp.put("title", name);
temp.put("intent", intent);
data.add(temp);
}

这就是用 title 填充 Map 的方式,正如您在上面看到的那样,它被映射到布局 XML 中的 @android/text1

我想将每个 TextView 的内容描述设置为与 name 相同的值。我尝试过的事情:

(1):遍历生成的 ListView 的子项并在每个子项上调用 item.setContentDescription(xxx)。这没有用;显然,此时我正在遍历它们不可见/不可访问/不存在的项目,并且无论哪种方式尝试调用 setContentDescription 都会失败。

(2):为这个修改后的 TextView 创建一个新的布局 XML,其中包括 android:contentDescription。布局 XML 如下所示

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:contentDescription="@android:id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight">
</TextView>

然后我确保将“contentDescription”添加到 addItem 返回的 Map 中,并将初始化代码更改为如下所示:

    setListAdapter(new SimpleAdapter(this, getData(path),
R.layout.simple_list_item_with_desc, new String[] { "title", "contentDescription" },
new int[] { android.R.id.text1, android.R.id.text2 }));

换句话说,我试图从我的静态数据映射中获取“contentDescription”以填充 android.R.id.text2 定义的布局 XML 中的字段。

这也不起作用——没有失败,但是当我在 uiautomatorviewer 中检查列表时,没有可用的内容描述。这可能是因为我不明白 android.R.id.text2 的意思,除了作为某种值的占位符之外。我还尝试了 android.R.id.content,结果相同。

所以我的问题是,如何动态设置这些 TextView 的 contentDescription?

最佳答案

I want to set the content description of each of the TextViews with the same value as name. Things I've tried:

(1): Looping through child items of the ListView that is generated and calling item.setContentDescription(xxx) on each of them. This didn't work; apparently at the point I'm looping through the items they're not visible/accessible/existent and either way trying to call setContentDescription blows up.

您是对的,您的第一个方法失败了,因为尚未创建任何 TextView。问题是它们从不同时可见(除非您只有少数几行并且它们无需滚动就适合屏幕。)

解决方案:扩展 SimpleAdapter 并覆盖 getView()。每次显示一行时都会调用此方法。它使您可以访问 TextView,因此您可以在此处调用 setContentDescription()

关于android - 填充 TextView 列表时动态设置 contentDescription,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15164439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com