Java – A better way to edit text with clarity

A better way to edit text with clarity… here is a solution to the problem.

A better way to edit text with clarity

I was just wondering if there is any better way to clear EditText in Android Java :

editText.getText().clear();

or

editText.setText("");

Are there any significant differences in efficiency? I saw some posts about liquidation, but no one explained the pros and cons.

Solution

clear() is a method of the Editable interface, calling getText() results in the implementation > SpannableStringBuilder is:

public void clear() {
    replace(0, length(), "", 0, 0);
    mSpanInsertCount = 0;
}

Without going into details, the method call replace(0, length(), "", 0, 0) simply replaces any string at index 0 with a zero-length string with length().

So I guess it’s the same as setText("").

Related Problems and Solutions