通过ContentDescription从textview获取文本

时间:2018-09-05 19:56:35

标签: java android

我有很多由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 

  }

1 个答案:

答案 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));
 }