How to remove focus without setting focus to another control?

后端 未结 11 1756
终归单人心
终归单人心 2020-11-28 22:02

I like my UIs to be intuitive; each screen should naturally and unobtrusively guide the user on to the next step in the app. Barring that, I strive to make things as confusi

相关标签:
11条回答
  • 2020-11-28 22:05

    Old question, but I came across it when I had a similar issue and thought I'd share what I ended up doing.

    The view that gained focus was different each time so I used the very generic:

    View current = getCurrentFocus();
    if (current != null) current.clearFocus();
    
    0 讨论(0)
  • 2020-11-28 22:06
    android:focusableInTouchMode="true"
    android:focusable="true"
    android:clickable="true"
    

    Add them to your ViewGroup that includes your EditTextView. It works properly to my Constraint Layout. Hope this help

    0 讨论(0)
  • 2020-11-28 22:07

    android:descendantFocusability="beforeDescendants"

    using the following in the activity with some layout options below seemed to work as desired.

     getWindow().getDecorView().findViewById(android.R.id.content).clearFocus();
    

    in connection with the following parameters on the root view.

    <?xml
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:descendantFocusability="beforeDescendants" />
    

    https://developer.android.com/reference/android/view/ViewGroup#attr_android:descendantFocusability

    Answer thanks to: https://forums.xamarin.com/discussion/1856/how-to-disable-auto-focus-on-edit-text

    About windowSoftInputMode

    There's yet another point of contention to be aware of. By default, Android will automatically assign initial focus to the first EditText or focusable control in your Activity. It naturally follows that the InputMethod (typically the soft keyboard) will respond to the focus event by showing itself. The windowSoftInputMode attribute in AndroidManifest.xml, when set to stateAlwaysHidden, instructs the keyboard to ignore this automatically-assigned initial focus.

    <activity
        android:name=".MyActivity"
        android:windowSoftInputMode="stateAlwaysHidden"/>
    

    great reference

    0 讨论(0)
  • 2020-11-28 22:07

    I tried to disable and enable focusability for view and it worked for me (focus was reset):

    focusedView.setFocusable(false);
    focusedView.setFocusableInTouchMode(false);
    focusedView.setFocusable(true);
    focusedView.setFocusableInTouchMode(true);
    
    0 讨论(0)
  • 2020-11-28 22:07

    Try the following (calling clearAllEditTextFocuses();)

    private final boolean clearAllEditTextFocuses() {
        View v = getCurrentFocus();
        if(v instanceof EditText) {
            final FocusedEditTextItems list = new FocusedEditTextItems();
            list.addAndClearFocus((EditText) v);
    
            //Focus von allen EditTexten entfernen
            boolean repeat = true;
            do {
                v = getCurrentFocus();
                if(v instanceof EditText) {
                    if(list.containsView(v))
                        repeat = false;
                    else list.addAndClearFocus((EditText) v);
                } else repeat = false;
            } while(repeat);
    
            final boolean result = !(v instanceof EditText);
            //Focus wieder setzen
            list.reset();
            return result;
        } else return false;
    }
    
    private final static class FocusedEditTextItem {
    
        private final boolean focusable;
    
        private final boolean focusableInTouchMode;
    
        @NonNull
        private final EditText editText;
    
        private FocusedEditTextItem(final @NonNull EditText v) {
            editText = v;
            focusable = v.isFocusable();
            focusableInTouchMode = v.isFocusableInTouchMode();
        }
    
        private final void clearFocus() {
            if(focusable)
                editText.setFocusable(false);
            if(focusableInTouchMode)
                editText.setFocusableInTouchMode(false);
            editText.clearFocus();
        }
    
        private final void reset() {
            if(focusable)
                editText.setFocusable(true);
            if(focusableInTouchMode)
                editText.setFocusableInTouchMode(true);
        }
    
    }
    
    private final static class FocusedEditTextItems extends ArrayList<FocusedEditTextItem> {
    
        private final void addAndClearFocus(final @NonNull EditText v) {
            final FocusedEditTextItem item = new FocusedEditTextItem(v);
            add(item);
            item.clearFocus();
        }
    
        private final boolean containsView(final @NonNull View v) {
            boolean result = false;
            for(FocusedEditTextItem item: this) {
                if(item.editText == v) {
                    result = true;
                    break;
                }
            }
            return result;
        }
    
        private final void reset() {
            for(FocusedEditTextItem item: this)
                item.reset();
        }
    
    }
    
    0 讨论(0)
  • 2020-11-28 22:08
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/ll_root_view"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    
    LinearLayout llRootView = findViewBindId(R.id.ll_root_view);
    llRootView.clearFocus();
    

    I use this when already finished update profile info and remove all focus from EditText in my layout

    ====> Update: In parent layout content my EditText add line:

    android:focusableInTouchMode="true"
    
    0 讨论(0)
提交回复
热议问题