简体   繁体   中英

how should I add a TextChangedListener to an EditText component?

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"

        />

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
}

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() {}

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 
    } 

});

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) {

    }
});

With ButterKnife

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

PS

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM