2016-07-04 47 views
0

嗨,大家好,我正在將一個應用程序與動態子項View集成在父項View中。如何從父視圖中獲取自定義子視圖的值android

我的代碼在父視圖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> 

這是我在Activity添加子視圖的代碼。我的孩子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> 

我已經使用此代碼來保存值。

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

如何爲每個孩子View得到EditText值。

回答

2

你可以給一個ID爲View

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

在此之後,你可以做這樣的事情讓您EditText文字:

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

由於其工作 –

0

只需使用for循環的addLayout(父視圖)來獲取子視圖,如。

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

然後設置ID爲意見添加視圖像view.setId(id);

相關問題