如何在CardView中从TextView获取文本?

时间:2017-08-07 09:13:19

标签: java android gettext android-cardview cardview

CardView内,我有一个LinearLayout,其中有三个TextViews,我想从这些TextView中获取文字。我的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>

然后我想迭代通过父LinearLayout中的所有CardView并获取TextViews。像这样:

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

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

3 个答案:

答案 0 :(得分:1)

这里有很多选择。
1。为TextViews提供id
2。使用ID为for的LinearLayout中的innerLayout
3。当你的UI是动态的,你需要从防御中获取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;
        }
    }

答案 1 :(得分:0)

只需提供TextView

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

答案 2 :(得分:0)

它不起作用,因为textview位于另一个ViewGroup,它是@ + id / innerLayout ..

我为你做了一个简单的功能..

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
       if (string == " " || string == "  ") &&  range.location == 0
       {
           return false
       }

       return true
}

你可以像这样使用..

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

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