我有很多由while循环生成的视图(textview
)和所有相同的ID,我试图通过tag
将内容获取到textview中,但是tag
仅提供了第一个视图为此,我使用了setContentDescription ...但是我不知道如何在另一种方法中通过textview
通过ContentDescription
获取内容
我的生成视图:
...............
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
}
答案 0 :(得分:0)
/**
* 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));
}