Skip to content Skip to sidebar Skip to footer

A Better Way To Onclick For Edittext Fields?

I have an EditText field, suppose the user has already entered text into it. Then the user wants to come back to edit the text again: the feature I want for this EditText field is

Solution 1:

In General

You can achieve what you want to do via a combination of onFocus and clearing the text field, similar to what the two commenters under your post already suggested. A solution would look like this:

EditTextmyEditText= (EditText) findViewById(R.id.myEditText);
myEditText.setOnFocusChangeListener(newOnFocusChangeListener() {
        
    @OverridepublicvoidonFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
        // Always use a TextKeyListener when clearing a TextView to prevent android// warnings in the log
        TextKeyListener.clear((myEditText).getText());
                
        }
    }
});

Please always use a TextKeyListener to "clean" EditText, you can avoid a lot of android warnings in the log this way.

But...

I would much rather recommend you to simply set the following in your xml:

<EditTextandroid:selectAllOnFocus="true"/>

Like described here. This way your user has a much better UI-feeling to it, he or she can decide on his/her own what to do with the text and won't be annoyed because it clears out every time!

Post a Comment for "A Better Way To Onclick For Edittext Fields?"