2

I'm new to android programming. I encountered a problem and I'm having a hard time applying the solution given in this topic: How to format the input of EditText when typing with thousands separators (,) in Android?

so far I have made a seperate Java file and pasted the main code in it but I can't find out how to "add editText.addTextChangedListener(new NumberTextWatcher(editText)); to my EditText component". my XML EditText looks like this:

        <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:id="@+id/editText"
        android:layout_weight="1"

        />
Zoe
  • 27,060
  • 21
  • 118
  • 148
mdehghani
  • 504
  • 1
  • 7
  • 23

5 Answers5

20

You can also use Butterknife for this

@OnTextChanged(value = R.id.etName, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
public void nameChanged(CharSequence text) {
    //do stuff
}

UPDATE: Kotlin Extension

fun EditText.afterTextChanged(onAfterChangeText: (String) -> Unit) {
    this.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun afterTextChanged(editable: Editable?) {
            onAfterChangeText(editable.toString())
        }
    })
}

Usage:

editText.afterTextChanged {
//do stuff
}

Kotlin without Extension:

editText.doAfterTextChanged {
    //do stuff
}
Dantalian
  • 561
  • 6
  • 15
2

First you need to find the EditText element in your Activity or Fragment like:

EditText editText = (EditText) findViewById(R.id.editText);

Then you should be able to do

editText.addTextChangedListener()

I recommend you take a look at butterknife library, http://jakewharton.github.io/butterknife/. It makes your code more organized if you have a lot of listeners.

You can simply do

@OnTextChanged(R.id.editText)
public void listener() {}
liuyl
  • 165
  • 7
1

First define the EditText variable as an instance variable in your class

private EditText mEditText; 

Then in some place such as onCreate or onViewCreated you can initialize your EditText and add the listener.

mEditText = (EditText)findViewById(R.id.edit_text);
mEditText.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {

    }

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

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // Get changed text s here and use it 
    } 

});
Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256
0

In Activity, get editText reference:

EditText et = (EditText) findViewById(R.id.editText);
et.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
});
Akshay Bhat 'AB'
  • 2,690
  • 3
  • 20
  • 32
0

With ButterKnife

@OnTextChanged(value = { R.id.firstNameEditText, R.id.lastNameEditText },
    callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void inputName(EditText editText, Editable editable) {
// Greet user...
}

P.S.

http://craftedcourses.io/all-about-butter-knife-part-1/

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121