【问题标题】:How to get value from dynamic EditText and store the values in a integer array?如何从动态 EditText 中获取值并将值存储在整数数组中?
【发布时间】:2019-07-09 19:12:51
【问题描述】:

我正在测试一个新应用程序,我在该应用程序上实现了动态 EditText,但遇到了一个问题。我将 Editexts 添加到 List 数组中,我试图从 EditText 中获取值并将其保存到整数数组中,但是当我单击一个按钮时,我得到的是空值。

我的代码:

List<EditText> all_value1 = new ArrayList<EditText>();
TextView textView = new TextView(this);
for (i = 0; i < 5; i++) {
    editText1 = new EditText(this);
    editText1.setInputType(TYPE_CLASS_NUMBER);
        all_value1.add(editText1);  
    linearlayout.addView(editText1);
}

int a[] = new int[all_value1.size()]

for(int i = 0; i < all_value.size(); i++) {
    a[i] = Integer.parseInt(all_value1.get(i).getText().toString());
}

Button b = new Button(this);
b.setOnClickListener(new View.OnCLickListener() {
    @override
    public void onClick(View v) {
        textView.setText(String.valueOf(a[0])); //I dont want to use all_value1.get(0).getText().toString()
    }
})

我的期望是将所有的 edittexts 值存储到一个整数数组中,单击按钮时我应该从该数组中获取一个特定的值。

【问题讨论】:

    标签: android arraylist dynamic android-edittext


    【解决方案1】:

    假设您在 onCreate() 方法中编写了上述代码,最初您的所有编辑文本都是空的,因此您的 Integer.parseInt(all_value1.get(i).getText().toString()) 将抛出 NumberFormatException
    尝试捕获解决方案,例如:

    for (int i = 0; i < all_value1.size(); i++) {
                try {
                    c[i] = Integer.parseInt(all_value1.get(i).getText().toString());
                } catch (Exception e) {
                    c[i] = 0;
                }
     }
    

    没有用,因为这将使用 0 初始化完整的数组 a[],如果您希望发生某些事情(即更改当您在 EditText 中键入内容时,相应 a[i] 的值。

    一个简单的解决方案是使数组不是整数类型,而是 EditText 类型本身(如果您真的不想在每次单击按钮时都使用 get() 函数)。

    试试下面的代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        List<EditText> all_value1 = new ArrayList<>();
        final TextView textView = new TextView(this);
        EditText editText1;
        LinearLayout linearLayout = findViewById(R.id.ll);
        linearLayout.addView(textView);
        for (int i = 0; i < 5; i++) {
            editText1 = new EditText(this);
            editText1.setInputType(TYPE_CLASS_NUMBER);
            all_value1.add(editText1);
            linearLayout.addView(editText1);
        }
    
        final EditText a[] = new EditText[all_value1.size()];
    
        for (int i = 0; i < all_value1.size(); i++) {
            a[i] = all_value1.get(i);
        }
    
        Button b = new Button(this);
        linearLayout.addView(b);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(a[0].getText().toString());
            }
        });
    }
    

    如果您想要一个 int 值,您可以在 EditText 中输入内容后使用Integer.parseInt(a[i].getText().toString())。 此解决方案将使按钮发挥作用,如果不满意,您必须将侦听器分配给 EditText。

    【讨论】:

    • 我想将这些文本视图值存储到另一个名为 int edittextvalues[] = new int[5]; 的数组中这就是我要问的问题,如果我这样做,我会得到 numberformatexcsption
    • 这是因为所有的工作(数组的初始化和赋值)都是在 onCreate() 被调用时完成的。最初 EditText 中没有任何内容,因此很明显您会遇到异常,因为 EditText 中的字符串是 "" ,它不是整数。无论您想对整数值做什么,只需将 EditText 数组传递给可以使用 Integer.parseInt(a[i].getText().toString()) 的函数。如果您不确定何时计算什么,请研究 android 活动生命周期。
    猜你喜欢
    • 2012-10-04
    • 2015-01-22
    • 1970-01-01
    • 2019-10-13
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多