简体   繁体   English

如何从Android的父视图中的自定义子视图中获取价值

[英]how to get value from custom child view in parent view in android

Hi guys I am integrating an app with dynamic child View in a parent View . 嗨,大家好,我正在将应用与动态子View集成到父View

My code to adding child View in parent view XML file. 我在父视图XML文件中添加子View代码。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/add_view"
            android:orientation="vertical">
        </LinearLayout>
</ScrollView>

This is my code for add child view in Activity . 这是我在Activity添加子视图的代码。

for (int i = 0;i < BloodInstance.getInstance().tempBloodResultDetails.size();i++){
            View view = LayoutInflater.from(this).inflate(R.layout.child_view, null);
            BloodResultDetails details = BloodInstance.getInstance().tempBloodResultDetails.get(i);
            resultName = (TextView) view.findViewById(R.id.child_name);
            resultRange = (TextView) view.findViewById(R.id.child_range);
            resultGetRange = (EditText) view.findViewById(R.id.child_get_range);

            resultName.setText(details.getName());
            resultRange.setText("Normal Range "+details.getMinRange()+" to "+details.getMaxRange()+" "+details.getUnit());

            addLayout.addView(view);
        }

Here is my child View XML file. 这是我的孩子的View XML文件。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_margin="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    app:cardCornerRadius="15dp">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:background="#e1e1e1"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/child_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:gravity="center"
            android:textColor="#000"
            android:textSize="20dp"
            android:typeface="serif"/>
        <TextView
            android:id="@+id/child_range"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#000"
            android:text="Normal Range"
            android:gravity="center"/>
        <EditText
            android:id="@+id/child_get_range"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:hint="Your Range"
            android:padding="5dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:background="@drawable/box"/>

    </LinearLayout>
</android.support.v7.widget.CardView>

I have use this code to save the values. 我已使用此代码保存值。

saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0;i < BloodInstance.getInstance().tempBloodResultDetails.size();i++){
                    String result = resultGetRange.getText().toString();
                    Log.e("Result",result);
                    resultUserRange.add(result);
                }
            }
        });

How to get value from EditText for each child View . 如何从EditText获取每个子View

You could give an ID to the View : 您可以给View指定一个ID:

View view = LayoutInflater.from(this).inflate(R.layout.child_view, null);
int id = View.generateViewId();
view.setId(id);

After that you could do something like this to get the text from your EditText : 之后,您可以执行以下操作从EditText获取文本:

View parent = findViewById(id);
EditText child = (EditText) parent.findViewById(R.id.child_get_range);
String text = child.getText().toString();

Simply use the for-loop for addLayout(parent view) to get the child view, like. 只需对addLayout(parent view)使用for循环即可获得子视图,例如。

for(int ij=0;ij<addLayout.getChildCount();ij++)
{
    //get user view here
}

then set id for views in add views like that view.setId(id); 然后在类似view.setId(id);添加视图中设置视图的view.setId(id);

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

相关问题 Android:如何从子自定义视图调用父Activity方法? - Android: How to call a parent Activity method from a child custom view? 将自定义属性的值从父视图级联到子视图? - Cascade the value of a custom attribute from a parent view to a child view? Android:在自定义视图中获取父级布局宽度以设置子级宽度 - Android: get parent layout width in custom view to set child width 如何在自定义视图类中获取“活动”子视图(Android) - How to get Activity child view in custom view class (Android) 如何在Android的自定义列表视图中从TextView获取值 - How to get the value from TextView in a custom list view in android 如何在父级活动类(Android)中获得子级活动视图? - How to get child Activity View in Parent Activity Class(Android)? Android如何从子视图中获取祖先视图? - Android how to get an ancestor view from a child view? 如何将单击事件从父(ViewGroup)委托给子(View)android? - How to delegate click event from parent (ViewGroup) to child (View) android? android将事件从子视图传递到父视图 - android passing events from child view to parent 使子视图在Android中的父视图之前获得焦点 - Make child view get focus before the parent view in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM