2017-08-07 87 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); 

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

相關問題