Android Multiple EditText can't implement listener

北城以北 提交于 2019-12-11 15:35:10

问题


hi I'm having a few problems understanding how to control my listeners in bulk. I've managed to get a few working from Stack Overflow but EditText is driving me nutts.

I have 3 editTexts and they all work happily if I idenpentatly enter the code, but I want to wrap them all up into one method with a switch case.

at the moment my code for one edittext looks like so (and with two more its a bit messy)

intTextValue = (EditText)findViewById(R.id.intervalValue);
      intTextValue.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
Double.parseDouble(intTextValue.getText().toString());

                if (textViewTouchIsHuman == true) {
                    intSeekValue = Double.parseDouble(intTextValue.getText().toString());
                    calculateWorkings();
                }
                textViewTouchIsHuman = true;    
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

            }

          });

I've tried using setOnKeyListener, setOnClickListern, addtextchangedlistenr but I can't get any of them to work? I hope that question makes senese. thank in adv.


回答1:


Why not make a TextWatcher outside of the assigning method addTextChangedListener() and then assign that TextWatcher to multiple EditText objects.

For instance:

TextWatcher tw = new TextWatcher();
intTextValue.addTextChangedListener(tw);
otherEditText.addTextChangedListener(tw);
...


来源:https://stackoverflow.com/questions/9762661/android-multiple-edittext-cant-implement-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!