Android Multiple EditText can't implement list

2019-09-06 11:15发布

问题:

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);
...