如何在使用android中的textwatcher从edittext完成编辑后,才能将值添加到hashmap

时间:2014-07-21 12:28:53

标签: java android textwatcher

我有多个具有edittexts的LinearLayouts实现了textwatcher,所以当我输入任何值时它直接放到hashmap,但是我想在完成编辑后添加一个完整的值,我的代码如下:

TextWatcher textwatcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            calculateInvoice();
            HashMap<String, String> descMap = new HashMap<String, String>();
            descMap.put("qty", et_qty.getText().toString() + "");
            descMap.put("upriz", et_unit_prize.getText().toString() + "");
            descMap.put("amt", subtotal + "");

            System.out.println("::::::::::::::::::MAP VALUES::::::::::" + descMap);

            descList.add(descMap);

        }
    };

1 个答案:

答案 0 :(得分:0)

让你有5个在xml文件中定义的editText视图。说它们是editText0,editText1 ... editText4。

现在在您的Activity类中,

1

构造一个String Array:String [] Data = new String [5]

2

分别为每个EditTexts添加Text观察器。喜欢:

editText0.addTextChangedListener(new TextWatcher() {

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

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

            @Override
            public void afterTextChanged(Editable Token) 
            {
                final int index = 0; // as this Text Watcher is specifically for editText0
                Data[index] = Token.toString();

            }
        });

3

同样将TextWatcher添加到剩余的4个EditTextViews中。