我取决于Android的用户输入成功创建EditTexts的,也是我所赋予他们的唯一的ID的使用 SETID()
方法。
I have successfully created EditTexts depending on the user input in Android, and also I have assigned them unique ID's using setId()
method.
现在我想要做的是从动态创建获取值的EditText
S当用户点击一个按钮,然后将其存储在字符串变量所有的人。从具有标识的EditText'1'即值应保存在String类型STR1,等等取决于EditTexts的数目
Now what I want to do is to get values from the dynamically created EditText
s when the user tap a button, then store all of them in String variables. i.e. value from EditText having id '1' should be saved in str1 of type String, and so on depending on the number of EditTexts.
我使用的getId()
和的gettext()。的toString()
方法,但似乎有点棘手的......我不能EditText上的每个值分配给一个字符串变量。当我尝试做一个 NullPointerException异常
时,如果它是不是在没有用户输入的数据显示的情况下,我举杯显示出来。
I am using getid()
, and gettext().toString()
methods but it seems a bit tricky... I cannot assign each value of EditText to a String variable. When I try to do that a NullPointerException
occurs, and if it is not the case where no user input data is shown, I display it in a toast.
继承人,在code:
EditText ed;
for (int i = 0; i < count; i++) {
ed = new EditText(Activity2.this);
ed.setBackgroundResource(R.color.blackOpacity);
ed.setId(id);
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linear.addView(ed);
}
我如何从现在的每一个的EditText值传递给每一个不同的字符串变量?如果一些机构可以帮助一个样品code这将是很好。
How do I now pass the value from each EditText to each different string variable? If some body could help with a sample code it would be nice.
在每次迭代你重写 ED
变量,所以当循环结束 ED
仅指向最后创建的实例的EditText
In every iteration you are rewriting the ed
variable, so when loop is finished ed
only points to the last EditText instance you created.
您应该所有引用存储所有EditTexts:
You should store all references to all EditTexts:
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < count; i++) {
ed = new EditText(Activity2.this);
allEds.add(ed);
ed.setBackgroundResource(R.color.blackOpacity);
ed.setId(id);
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linear.addView(ed);
}
现在 allEds
列表保持引用的所有EditTexts,这样你就可以遍历,并得到所有的数据。
Now allEds
list hold references to all EditTexts, so you can iterate it and get all the data.
更新:
根据要求:
String[] strings = new String[](allEds.size());
for(int i=0; i < allEds.size(); i++){
string[i] = allEds.get(i).getText().toString();
}
上一篇:如何使用命令执行DEX文件中的android?如何使用、命令、文件、DEX
下一篇:在Android应用程序Toolbar.setTitle方法没有效果 - 应用程序名称显示为标题应用程序、名称、效果、标题