5

I'm just playing around with Kotlin on my project and I came to strange problem... When trying to convert custom EditText Android studio stops responding. When I tried to convert it part by part, it stops responding when converting this piece of code:

private TextWatcher editor = new TextWatcher() {

    long newMicro = 0;

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        s = s.toString().replace(".", "")
                .replace(",", "")
                .replace("$", "");

        try {
            newMicro = Long.parseLong(s.toString());
        } catch (Exception e) {
            newMicro = 0;
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {
        removeTextChangedListener(editor);
        setMicroUnits(newMicro);
        addTextChangedListener(editor);
    }
};

Have you experienced such a behavior? I'm unable to reimplement this TextWatcher in Kotlin since I cannot perform neither

CharSequence.toString().replace()

nor

CharSequence.replace()

Any idea how to implement custom TextWatcher in Kotlin? This is code I've prepared:

val editor = object : TextWatcher {
    override fun afterTextChanged(p0: Editable?) {

    }

    override fun beforeTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {

    }

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

}

Edit: Problem occurs while working with kotlin version 1.1.2-4 on Android Studio 3 Preview 6

2 Answers 2

4

So it looks like it's not only my problem, that TextWatcher hangs while converting. It stops responding every time. My problem was, that I was unable to use Kotlins String.replace()

Solution was just to use proper versions of Kotlin AndroidStudio 3.0 preview 6 with kotlin_version = '1.1.3-2'

Sign up to request clarification or add additional context in comments.

Comments

3

I've just tried to convert your code to Kotlin in Android Studio and it hanged as well. I assume it's problem with nullable parameters in TextWatcher interface methods.

Anyway, this piece of code must be what you are looking for:

private val editor = object : TextWatcher {

    var newMicro: Long = 0

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        val text = s.toString().replace(".", "")
                .replace(",", "")
                .replace("$", "")

        try {
            newMicro = java.lang.Long.parseLong(text)
        } catch (e: Exception) {
            newMicro = 0
        }
    }

    override fun afterTextChanged(s: Editable?) {
        removeTextChangedListener(this)
        setMicroUnits(newMicro)
        addTextChangedListener(this)
    }
}

1 Comment

Sorry for your time, only problem was usage of incorrect kotlin version. Studio sill freezes and nullable parameters are not problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.