简体   繁体   English

从editText获取内容

[英]Getting content from editText

In my application, i have a tableLayout with many editTexts in it. 在我的应用程序中,我有一个tableLayout里面有很多editTexts。 When i click "save"button, i want to access all the values entered in editTexts. 当我单击“保存”按钮时,我想访问在editTexts中输入的所有值。 I have assigned IDs in runtime while creating the table. 我在创建表时在运行时分配了ID。 Now how can i access the values from editTexts when "save" button is clicked...? 现在,当单击“保存”按钮时,如何从editTexts中访问值? I have used something like below to assign IDs, 我已经使用了以下类似的方法来分配ID,

for(int i=0;i< no_of_rows ;i++)
  for(int j=0;j<5;j++)
  {   
    ...............
    assignment.setId(i+j);
    .............
  }

Could anyone suggest a solution..? 有人可以建议解决方案吗?

How about something like: 怎么样:

ArrayList<String> strings = new ArrayList<String>();
for(int i=0;i< no_of_rows ;i++)
  for(int j=0;j<5;j++)
  {   
    EditText text = (EditText)ActivityName.this.findViewById(i+j);
    strings.add(text.getText().toString());
  }
}

This would give you all of the values from all of the texts in one big array. 这样一来,您就可以将所有文本中的所有值都放在一个大数组中。 If that's not what you want, let me know and I'll see if I can adjust the code. 如果那不是您想要的,请告诉我,我将看看是否可以调整代码。

Other nice solution is to cycle through all childrens of some Layout, so you don't need to set any special IDs. 另一个不错的解决方案是遍历某个Layout的所有子项,因此您无需设置任何特殊的ID。 Just try: 试一试:

LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
child_count = layout.getChildCount();

for (int i=0; i<child_count; i++)
{
    EditText text = (EditText) layout.getChildAt(i);
    // do work with text
}

With some other code, you can do this for any other layout hierarchy. 使用其他代码,您可以对任何其他布局层次结构执行此操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM