0

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.

Purplemonkey
  • 1,927
  • 3
  • 27
  • 39

1 Answers1

2

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);
...
AJcodez
  • 31,780
  • 20
  • 84
  • 118
  • 1
    Hi, thanks that does look like what I need, the problem I have now though, is how to switch the code to find the edittext that is being changed? – Purplemonkey Mar 18 '12 at 22:16
  • Yeah I dont see an easy way to do that. You could check which one has focus, save the string values in each and see if they have been changed in onTextChange and save which EditText is the one to use, or some other way. Then call a method where you pass in the right EditText and use a switch statement. – AJcodez Mar 19 '12 at 02:29
  • my word, so I'm not just lacking on knowledge then, there is simply no easy way (like other listeners i.e. Seekbar, Spinner, RadioGroup etc) to do this. I will have a look at coding up your suggestion, thanks for your time :) – Purplemonkey Mar 19 '12 at 09:33