AutoCompleteTextView always keeps focus

I have 2 AutoCompleteTextViewsin activity (LinearLayout) and several additional controls(radio groups, buttons, etc.). Somehow AutoCompleteTextViews never losing focus.

As an example: The user clicks on the AutoCompleteTextView, the control receives focus. Thus, the cursor starts flashing, the autocomplete drop-down list and the keyboard are displayed. It is perfectly. However, if user now clicks on of the radio buttons(or another control), the cursor in the AutoCompleteTextView is still blinkingand keyboard is still displayed.

How to make focus automatically disappear?

EDIT: xml code

                <AutoCompleteTextView
                android:id="@+id/ediFrom"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:singleLine="true"
                android:text="" />
+4
source share
3 answers

, , -

android:focusable="true" 
android:focusableInTouchMode="true"

AutoCompleteTextView (, LinearLayout ..)

+6

android:focusableInTouchMode="true" each view

<AutoCompleteTextView
      android:id="@+id/ediFrom"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:singleLine="true"
      android:focusableInTouchMode="true"
      android:text="" />

http://android-developers.blogspot.in/2008/12/touch-mode.html

+2

focusable ( , ), :

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    View v = getCurrentFocus();
    if (v instanceof EditText) {
        int scrcoords[] = new int[2];
        v.getLocationOnScreen(scrcoords);
        // calculate the relative position of the clicking position against the position of the view
        float x = event.getRawX() - scrcoords[0];
        float y = event.getRawY() - scrcoords[1];

        // check whether action is up and the clicking position is outside of the view
        if (event.getAction() == MotionEvent.ACTION_UP
                && (x < 0 || x > v.getRight() - v.getLeft()
                || y < 0 || y > v.getBottom() - v.getTop())) {
            if (v.getOnFocusChangeListener() != null) {
                v.getOnFocusChangeListener().onFocusChange(v, false);
            }
        }
    }
    return super.dispatchTouchEvent(event);
}

If you put this logic into your basic activity, it will shoot on any screen with the edit text onFocusChangewhen you click anywhere outside of it. By listening onFocusChange, you can clearFocuseither requestFocusin another performance. This is a hack more or less, but at least you don't need to customize any other elements on many layouts.

See http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent(android.view.MotionEvent)

+1
source

Source: https://habr.com/ru/post/1528150/


All Articles