gpt4 book ai didi

java - 通过 ContentDescription 从 TextView 中获取文本

转载 作者:太空宇宙 更新时间:2023-11-04 10:11:55 26 4
gpt4 key购买 nike

我有许多从 while 循环生成的 View (textview),并且所有相同的 id,我尝试通过 tag 将内容获取到 textview,但 tag 仅给出我使用 setContentDescription 的第一个 View ...但我不知道如何在另一种方法中通过 ContentDescription 获取内容 textview

我的生成 View :

    ...............
while (condition) {
final TextView dec1= (TextView) addView.findViewById(R.id.dec1);
final TextView dec2= (TextView) addView.findViewById(R.id.dec2);
final TextView dec3= (TextView) addView.findViewById(R.id.dec3);
dec_v1.setContentDescription("a"+dbid);
dec_v2.setContentDescription("b"+dbid);
dec_v3.setContentDescription("c"+dbid);
}
.................
public void getD(){
//here need to get textview content

}

最佳答案

/**
* Local cache of added items.
* Key - Description Text (ID of item), Value - Text
*/
private final Map<String, TextView> mActiveViews = new HashMap<>();

private method1() {
...............
while (condition) {
final TextView dec1= (TextView) addView.findViewById(R.id.dec1);
final TextView dec2= (TextView) addView.findViewById(R.id.dec2);
final TextView dec3= (TextView) addView.findViewById(R.id.dec3);

dec_v1.setContentDescription("a"+dbid);
mActiveViews.put("a"+dbid, dec1)
dec_v2.setContentDescription("b"+dbid);
mActiveViews.put("b"+dbid, dec2)
dec_v3.setContentDescription("c"+dbid);
mActiveViews.put("c"+dbid, dec3)
}
.................
}


public void getD() {
/* Access to all Active TextViews
*/
for (Map.Entry<String, TextView> entry : mActiveViews.entrySet()) {
System.out.println(entry.getKey() + "/" + entry.getValue());
}
/* Another sample with getting specific TextView
* by your Content Description (Key)
*/
System.out.println(mActiveViews.get("a" + dbid));
}

关于java - 通过 ContentDescription 从 TextView 中获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52192572/

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