1

I'm programming on Android and I want an easy way to do the things below without having to have a bunch of overrides and ugly code:

These are all examples:

  • When editText1 is changed I want a TextView to be updated with copy of what the user typed Same thing for editText2 up to editText10.
  • When editText11 is changed I want to multiply the number in it by 10 and put it In some TextView.
  • When editText12 becomes 0 I want some LinearLayouts to hide

Basically I want to be able to easily set up a listener and modify what kind of method the listener will trigger, without having a bunch of anonymous inner classes and other nasty stuff. Having many derivatives that each do their own predefined thing is OK, but I want to avoid repeating code and make it utilize polymorphism.

I tried really hard using interfaces, abstract methods, and other similar techniques but it just made my head go crazy.

Dharman
  • 30,962
  • 25
  • 85
  • 135
JamMaster
  • 1,446
  • 1
  • 14
  • 24
  • This is isn't so much about android dev as it is about inheritance, interfaces and extending. And i haven't read any books, its pretty easy even without a tutorial. – JamMaster Jul 18 '14 at 17:12
  • That basically is a TextWatcher. You can do whatever you want in it. You don't get more generic than that. – Gabe Sechan Jul 18 '14 at 17:13
  • Pro Tip: In English, `I` is always capitalized (when it's used to refer to yourself). – Robert Harvey Jul 18 '14 at 17:16
  • Robert Harvey I know I'm trying as hard as I can not to forget it. @Gabe Sechan I know how to set up a text lisener, I dont know how to make it so that I can have custom methods for each listener. – JamMaster Jul 18 '14 at 17:32
  • @jammaster You set up a different listener on each edit text. Then each edit text has its own custom method. You don't set up one and it applies to all edit text, and you aren't restricted to one TextWatcher object. – Gabe Sechan Jul 18 '14 at 17:37

2 Answers2

0

What you probably want is a TextWatcher

How to use the TextWatcher class in Android?

Here's an example:

http://www.learn-android-easily.com/2013/06/using-textwatcher-in-android.html

You can pass TextView objects into it and monitor the text text as it is input by the user. However, even this is probably pretty complex based on the requirements you are describing and the general unpredictable behavior of users in general.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
0

Why don't you try something like this for a custom TextWatcher with an event-listener callback you can use for after the text has changed:

public class CustomTextWatcher implements TextWatcher {

    public interface TextChangedEventListener{
        public void afterTextChanged(String newText);
    }

    private TextChangedEventListener eventListener;

    public CustomTextWatcher(TextChangedEventListener eventListener){
        this.eventListener = eventListener;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        eventListener.afterTextChanged(s.toString());
    }
}

Now for the Implementation...

EditText editText = (EditText)findViewById(R.id.my_et);
editText.addTextChangedListener(new CustomTextWatcher(new CustomTextWatcher.TextChangedEventListener() {
    public void afterTextChanged(String newText) {
        // set the text of the next EditText, which will trigger the next TextWatcher in the chain
    }
}));

You're still going to have to deal with an anonymous inner class, but there's not really a way around that if you want customization for each EditText. At least this will cut down on the number of inner classes from 3 to 1 and clean up the code you have to look at a bit.

Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • This seems a little pointless. Its a lot of code and an extra level of indirection to avoid adding two empty functions to some other class? – Gabe Sechan Jul 18 '14 at 17:18
  • Yes, I clearly stated that. But, regardless, it still looks cleaner than having 11 EditTexts each with 2 unused anonymous inner classes. – Cruceo Jul 18 '14 at 17:19
  • @Guardanis This is on the right track. This checks about half of what I tried to do. Thanks a lot. The problem is that I will probably need a few of those listeners for few different EditTexts. And to do that I have to repeat the above code. I want something where i can just call a method that takes in an EditText that is supposed to be listened to and a method that should be executed when the listener triggers. This way I would just need one call to setup multiple listeners. – JamMaster Jul 18 '14 at 17:51
  • If you're only watching text change events on a the first EditText in the chain (assuming the others aren't editable), then you can just create a method that changes the text of all the EditTexts based on the primary input. If you want each one to respond dynamically to what it receives, a single TextWatcher won't work, and will require new instances of a TextWatcher with customized implementation for each EditText you want to handle events for. The most dynamic approach is probably to create a new instance for each EditText and handle the result in the anonymous inner classes – Cruceo Jul 18 '14 at 18:30
  • I know it's not pretty, but the alternative is even messier and even minor changes would require heavy reworking of your code. This kind of solution would be a lot more maintainable, as any updates can be done at any point in the chain without causing you to refactor a majority of your code. – Cruceo Jul 18 '14 at 18:33