将EditText(Type:Number)输入到array []中

时间:2015-06-28 08:41:09

标签: java android arrays

我有一个EditText(输入类型:numberDecimal),我想继续添加值并将它们保存到数组中。 我怎样才能做到这一点 ?
举个例子:

EditText:"1234"
Array[0]=1;
Array[1]=2;
Array[2]=3;
Array[3]=4;

...谢谢

2 个答案:

答案 0 :(得分:2)

TextWatcher是Android Developer API提供的有用类。它可用于观看输入文本字段,您可以立即更新其他视图上的数据。它可以立即计算在文本字段中输入的字符数。

EditText et = (EditText) findViewById(R.id.EditText01);

        et.addTextChangedListener( new TextWatcher()
        {
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

                try
                {
                    char currentChar = arg0.charAt(arg1); // currently typed character
                }
                catch(Exception e)
                {
                    // error
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {

            }

            @Override
            public void afterTextChanged(Editable arg0) {

            }
        });

http://www.learn2crack.com/2014/02/android-textwatcher-example.html

答案 1 :(得分:0)

请使用ArrayList,因为您不知道用户输入的确切数字。

ArraList<String> list=new ArrayList<String>();

EditText et = (EditText) findViewById(R.id.EditText01);

    et.addTextChangedListener( new TextWatcher()
    {
        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            try
            {
                char currentChar = arg0.charAt(arg1); // currently typed character
                list.add(currentChar);// Add the character to the list.
            }
            catch(Exception e)
            {
                // error
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {

        }

        @Override
        public void afterTextChanged(Editable arg0) {

        }
    });