在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中獲取文本?
謝謝!它非常完美,特別是當我有多個CardView時。 – Sumsar9000
沒問題的人。 – Fr099y