[英]How to get data from many EditTexts when Its added in layout programmatically in android
I'm adding EditText in linear layout and it gives a view like that in image. 我在线性布局中添加EditText,它提供了类似于图像的视图。
I'm getting this view by using this code. 我通过使用此代码获得此视图。
public class SearchRecipe extends AppCompatActivity { LinearLayout parentLayout; ImageButton searchRecipe; private int EDITTEXT_ID = 1; private List<EditText> editTextList; EditText editTextItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search_recipe); setActionBar(); init(); searchRecipe.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText editTextItem = (EditText) parentLayout.findViewById(EDITTEXT_ID); for (int i = 0; i < editTextList.size(); i++) { Log.e("All Values=", editTextList.get(i).getText().toString()); Toast.makeText(SearchRecipe.this, editTextItem.getText().toString() + " ", Toast.LENGTH_SHORT).show(); } } }); } public void init() { parentLayout = (LinearLayout) findViewById(R.id.parent_layout); //make sure you have set vertical orientation attribute on your xml searchRecipe = (ImageButton) findViewById(R.id.search_button); editTextList = new ArrayList<EditText>(); TextView addMoreText = new TextView(this); addMoreText.setText("Add More Ingredients"); addMoreText.setGravity(Gravity.CENTER); addMoreText.setPadding(20, 20, 20, 20); addMoreText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.add, 0); addMoreText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editTextItem = new EditText(SearchRecipe.this); editTextItem.setId(EDITTEXT_ID); editTextList.add(editTextItem); EDITTEXT_ID++; editTextItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cross, 0); editTextItem.setPadding(20, 20, 20, 20); editTextItem.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { parentLayout.removeView(editTextItem); return true; } }); parentLayout.addView(editTextItem, 0); } }); parentLayout.addView(addMoreText); }
Now the only problem I'm facing is that. 现在我面临的唯一问题是那个。 I'm not getting the text from edittext properly. 我没有从edittext正确获取文本。 Let me Explain what I want to do. 让我解释一下我想做什么。
if you are createing edit text run time only for this purpose then there is no need of below tow lines 如果仅出于此目的而创建编辑文本运行时,则无需在下面拖行
editTextItem.setId(EDITTEXT_ID); editTextItem.setId(EDITTEXT_ID);
EDITTEXT_ID++; EDITTEXT_ID ++;
To retrive data from each edit box follow below things 要从每个编辑框中检索数据,请执行以下操作
for (EditText editText : editTextList) {
/* now you can get the value from Edit-text and save in the ArrayList
or you can append it in same string*/
yourArraList.add(editText.getText().toString()));
}
you can do like below if view inside fragment. 如果查看片段内部,则可以执行以下操作。
public static String getText(final Activity activity) {
final StringBuilder stringBuilder=new StringBuilder();
LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
ArrayList<String> msg = new ArrayList<String>();
for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
{
LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
EditText editText = (EditText) innerLayout.findViewById(R.id.meeting_dialog_et);
msg.add(editText.getText().toString());
}
for (int j=0;j<msg.size();j++)
{
stringBuilder.append(msg.get(j)).append(";");
}
Toast t = Toast.makeText(activity.getApplicationContext(), stringBuilder.toString(), Toast.LENGTH_SHORT);
t.show();
return stringBuilder.toString();
}
there is another way is make arraylist Edittexes and add each edittext when it added to layout. 还有另一种方法是使arraylist Edittexes并在将每个edittext添加到布局时添加它们。 then you can get like below: 那么您可以得到如下所示:
for (int i = 0; i < Edittexes.size(); i++) {
if (Edittexes.get(i) == view)
{
String text=Edittexes.get(i).getText();
}
}
Get the editext
from your list editTextList
从列表中获取editext
editTextList
String data = editTextList.get(index).getText().toString();
Add check for editTextList should not be null or empty. 添加对editTextList的检查不应为null或为空。
You can iterate over list using for-each loop 您可以使用for-each循环遍历列表
for (EditText editText : editTextList) {
// now you can get the value from Edit-text and save in the ArrayList
yourArraList.add(editText.getText().toString()));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.