2017-08-07 133 views
-1

CardView里面,我有一个LinearLayout,其中有三个TextViews,我想从这些TextView中获取文本。我的XML文件是这样的:如何从CardView中的TextView中获取文本?

<LinearLayout 
     android:id="@+id/defense" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     > 

     <android.support.v7.widget.CardView 
      android:id="@+id/cardCB" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom" 
      android:layout_weight="1" 
      app:cardBackgroundColor="@android:color/transparent" 
      app:cardElevation="0dp"> 

      <LinearLayout 
       android:id="@+id/innerLayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="Rannochia" 
        android:textAlignment="center" 
        android:textStyle="bold"/> 
       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="78" 
        android:textAlignment="center" 
        android:textStyle="bold"/> 
       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="CB" 
        android:textAlignment="center" 
        android:textStyle="bold"/> 

      </LinearLayout> 
     </android.support.v7.widget.CardView> 
     </LinearLayout> 

然后,我通过在父的LinearLayout所有CardViews要迭代并获得TextViews。事情是这样的:

 for (int i = 0; i < defense.getChildCount(); i++){ 
     String getName = ((CardView)defense.getChildAt(i)).getText().toString(); 
     id[i] = getName; 
    } 

但我不能够调用的方法getText().toString();这样。如何从CardView中的这些TextView中获取文本?

回答

0

这么多的选择在这里。
1.给ID为您TextViews
2.使用for在LinearLayout中ID为innerLayout
3.当你的用户界面是动态的,你需要从防守得到TextViews

for (int i = 0; i < defense.getChildCount(); i++){ 
     CardView card = defense.getChildAt(i); 
     ViewGroup viewGroup = ((ViewGroup)card.getChildAt(0)); 
     for(int j=0;j<viewGroup.getChildCount();j++){ 
      String getName = ((TextView)viewGroup.getChildAt(j)).getText().toString(); 
      id[i] = getName; 
     } 
    } 
+0

谢谢!它非常完美,特别是当我有多个CardView时。 – Sumsar9000

+0

没问题的人。 – Fr099y

0

只要给参考的TextView

for (int i = 0; i < defense.getChildCount(); i++){ 
     String getName = ((TextView)defense.getChildAt(i)).getText().toString(); 
     id[i] = getName; 
    } 
0

它不工作,因为TextView的是另一种的ViewGroup这是@ + ID/innerLayout ..

我做一个简单的功能,你在我心中..

private void loopViews(ViewGroup view) { 
for (int i = 0; i < view.getChildCount(); i++) { 
    View v = view.getChildAt(i); 

    if (v instanceof EditText) { 
     // will be executed when its edittext 
     Log.d("Check", "This is EditText"); 

    } else if (v instanceof TextView) { 
     // will be executed when its textview,, and get the text.. 
     TextView x = (TextView) v; 
     String aa = x.getText().toString(); 
     Log.d("Check", "This is TextView with text : " +aa); 

    } else if (v instanceof ViewGroup) { 

     // will be executed when its viewgroup,, and loop it for get the child view.. 
     Log.d("Check", "This is ViewGroup"); 
     this.loopViews((ViewGroup) v); 
    } 
} 

,你可以使用它像这样..

LinearLayout def = (LinearLayout) findViewById(R.id.defense); 
    loopViews(def); 

希望它可以帮助你.. :)

相关问题