2017-02-18 71 views
0

我已經動態添加了具有編輯字段的線性佈局,現在我想從這些編輯字段中獲取文本。這裏是我的代碼動態添加布局從線性佈局動態添加編輯文本中獲取文本

linearLayoutForm = (LinearLayout) findViewById(R.id.linearLayoutForm); 
     btnAdd.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       final LinearLayout newView = (LinearLayout)getLayoutInflater().inflate(R.layout.single_skill_row, null); 
       newView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
       ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove); 
       btnRemove.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         linearLayoutForm.removeView(newView); 
        } 
       }); 
       linearLayoutForm.addView(newView,0); 
      } 
     }); 

我試圖讓這段代碼的文本,但我得到了空指針異常。

register_btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      LinearLayout et2=(LinearLayout) linearLayoutForm.getChildAt(linearLayoutForm.getChildCount()); 
      EditText e = (EditText) et2.getChildAt(et2.getChildCount()); 
      String s=e.getText().toString(); 
}); 

這裏是single_skill_row的XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rowdetail" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/editDescricao" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.62" 
     android:ems="10" 
     android:inputType="text" 
     android:maxLines="1"> 
     <!--<requestFocus />--> 
    </EditText> 

    <ImageButton 
     android:id="@+id/btnRemove" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/btn_remove" 
     android:src="@drawable/ic_remove_black_18dp" /> 
</LinearLayout> 
+0

後你的logcat請 –

+0

你會添加你的xml文件single_skill_row.xml嗎? –

+0

已添加xml文件。 –

回答

0

你可以這樣說:

的所有類首先你添加一個字段:

... 
EditText myEditText; 
... 

然後添加你的LinearLayout後您的EDITTEXT存儲領域是這樣的:

... 
final LinearLayout newView = (LinearLayout)getLayoutInflater().inflate(R.layout.single_skill_row, null); 
myEditText = newView.findViewById(R.id.editDescricao); 
... 

後,在您的按鈕的onclick回調,你可以得到它的文字是這樣的:

register_btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
     String text = myEditText.getText().toString(); 
}); 
+0

從此我從第一個編輯字段中獲取文本。我想從動態添加的所有編輯字段中獲取文本 –

+0

您可以像這樣獲取它們。只需添加一個字段,當您膨脹視圖時存儲引用,從那些引用中獲取onClick回調中的文本 –

0

可以實現與findViewById

register_btn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

     LinearLayout et2=(LinearLayout) linearLayoutForm.getChildAt(linearLayoutForm.getChildCount()); 
     EditText e = (EditText) et2.findViewById(R.id.editDescricao); 
     String s=e.getText().toString(); 
}); 
+0

從此我從第一個編輯字段中獲取文本。我想從動態添加的所有編輯字段中獲取文本。 –

+0

它在onClick方法的第二行返回空指針異常。 –