简体   繁体   English

如何从CardView内部的TextView获取文本?

[英]How to get text from TextView inside a CardView?

Inside a CardView I have a LinearLayout with three TextViews in it and I want to get the text from these TextViews. CardView内部,我具有一个带有三个TextViewsLinearLayout ,我想从这些TextViews中获取文本。 My XML-file looks like this: 我的XML文件如下所示:

 <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>

Then I want to iterate through all CardViews in the parent LinearLayout and get the TextViews. 然后,我要遍历父级LinearLayout中的所有CardView并获取TextViews。 Something like this: 像这样:

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

But I'm not able to call the methods getText().toString(); 但是我无法调用方法getText().toString(); like this. 像这样。 How can I get the text from these TextViews inside the CardView? 我如何从CardView内部的这些TextView中获取文本?

So many options in here. 这里有很多选择。
1. Give id for your TextViews 1.给您的TextViews ID
2. Use for in LinearLayout with id innerLayout 2. for id为innerLayout LinearLayout中
3. When your UI is dynamic and you need to get TextViews from defense 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;
        }
    }

Just give the reference to TextView 只需提供对TextView的引用

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

It not works because the textview is inside another ViewGroup which is @+id/innerLayout.. 它不起作用,因为textview在另一个ViewGroup内部,该ViewGroup是@ + id / innerLayout。

i make a simple function for you.. 我为你做一个简单的功能。

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

and you can use it like this.. 您可以像这样使用它。

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

hope it can help you.. :) 希望它可以帮助您.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM