<legend id='FV8Cj'><style id='FV8Cj'><dir id='FV8Cj'><q id='FV8Cj'></q></dir></style></legend>

      • <bdo id='FV8Cj'></bdo><ul id='FV8Cj'></ul>

        <small id='FV8Cj'></small><noframes id='FV8Cj'>

        <tfoot id='FV8Cj'></tfoot>
      1. <i id='FV8Cj'><tr id='FV8Cj'><dt id='FV8Cj'><q id='FV8Cj'><span id='FV8Cj'><b id='FV8Cj'><form id='FV8Cj'><ins id='FV8Cj'></ins><ul id='FV8Cj'></ul><sub id='FV8Cj'></sub></form><legend id='FV8Cj'></legend><bdo id='FV8Cj'><pre id='FV8Cj'><center id='FV8Cj'></center></pre></bdo></b><th id='FV8Cj'></th></span></q></dt></tr></i><div id='FV8Cj'><tfoot id='FV8Cj'></tfoot><dl id='FV8Cj'><fieldset id='FV8Cj'></fieldset></dl></div>
      2. 如何从 Android 中每个动态创建的 EditText 获取数据?

        How to get data from each dynamically created EditText in Android?(如何从 Android 中每个动态创建的 EditText 获取数据?)

            <bdo id='zLk30'></bdo><ul id='zLk30'></ul>
                <tbody id='zLk30'></tbody>

              <small id='zLk30'></small><noframes id='zLk30'>

              • <tfoot id='zLk30'></tfoot>

                <i id='zLk30'><tr id='zLk30'><dt id='zLk30'><q id='zLk30'><span id='zLk30'><b id='zLk30'><form id='zLk30'><ins id='zLk30'></ins><ul id='zLk30'></ul><sub id='zLk30'></sub></form><legend id='zLk30'></legend><bdo id='zLk30'><pre id='zLk30'><center id='zLk30'></center></pre></bdo></b><th id='zLk30'></th></span></q></dt></tr></i><div id='zLk30'><tfoot id='zLk30'></tfoot><dl id='zLk30'><fieldset id='zLk30'></fieldset></dl></div>
                <legend id='zLk30'><style id='zLk30'><dir id='zLk30'><q id='zLk30'></q></dir></style></legend>
                  本文介绍了如何从 Android 中每个动态创建的 EditText 获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经根据 Android 中的用户输入成功创建了 EditText,并且我还使用 setId() 方法为它们分配了唯一 ID.

                  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 中获取值,然后将它们全部存储在 String 变量中.即来自 EditText 的具有 id '1' 的值应保存在 String 类型的 str1 中,依此类推,具体取决于 EditText 的数量.

                  Now what I want to do is to get values from the dynamically created EditTexts 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,如果不是没有显示用户输入数据的情况,我会在 toast 中显示它.

                  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.

                  这里,代码:

                  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 的值传递给每个不同的字符串变量?如果有人可以帮助提供示例代码,那就太好了.

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

                  这篇关于如何从 Android 中每个动态创建的 EditText 获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                      <legend id='UwhJt'><style id='UwhJt'><dir id='UwhJt'><q id='UwhJt'></q></dir></style></legend>

                      <small id='UwhJt'></small><noframes id='UwhJt'>

                        <bdo id='UwhJt'></bdo><ul id='UwhJt'></ul>
                        <tfoot id='UwhJt'></tfoot>
                          <tbody id='UwhJt'></tbody>

                            <i id='UwhJt'><tr id='UwhJt'><dt id='UwhJt'><q id='UwhJt'><span id='UwhJt'><b id='UwhJt'><form id='UwhJt'><ins id='UwhJt'></ins><ul id='UwhJt'></ul><sub id='UwhJt'></sub></form><legend id='UwhJt'></legend><bdo id='UwhJt'><pre id='UwhJt'><center id='UwhJt'></center></pre></bdo></b><th id='UwhJt'></th></span></q></dt></tr></i><div id='UwhJt'><tfoot id='UwhJt'></tfoot><dl id='UwhJt'><fieldset id='UwhJt'></fieldset></dl></div>