在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