jaygee
jaygee

Reputation: 25

how can full clear focus in editText , just like press button DONE on softkeyboard

I specify different backgrounds when edit text focus changed , but clearFocus() seem like no effect , because the background do not change to the right state .

Upvotes: 2

Views: 3228

Answers (2)

Lenoarod
Lenoarod

Reputation: 3620

Firstly, we can use cleareFocus method to remove the focus.

editTextView.clearFocus()

But when this method is called, as source code comment says:

When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after which the framework will give focus to this view.

so after you call it, the first element will stay get the focus. so we should set the touch-mode true in parent layout.

<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:layout_width="0px" 
    android:layout_height="0px"/>
<EditText android:id="@+id/et"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    />

Upvotes: 1

Ganesh Pokale
Ganesh Pokale

Reputation: 1594

Create one more EditText(edit_hidden) and set visibility GONE or INVISIBLE

When you want to clear Focus from your EditText(edit_your) then use this Code

 edit_your.clearFocus();
 edit_hidden.requestFocus();

Using Action Done Event

edit_your.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
               edit_your.clearFocus();
               edit_hidden.requestFocus();
        }
    return false;
    }
}); 

Upvotes: 2

Related Questions