How to remove focus from single editText

后端 未结 17 1383
南笙
南笙 2020-12-13 17:24

In my application I have a single EditText together with some TextViews, button and a spinner. My EditText receives focus since it is the only focu

相关标签:
17条回答
  • 2020-12-13 17:31

    For me this worked

    Add these attributes to your EditText

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

    After this in your code you can simply write

    editText.clearFocus()
    
    0 讨论(0)
  • 2020-12-13 17:32

    You just have to clear the focus from the view as

    EditText.clearFocus()
    
    0 讨论(0)
  • 2020-12-13 17:33

    Use the attached code to give the focus to "someone else", this is OK if you have a view where you simply want to dismiss the keyboard and release the focus, you don't really care about who gets it.

    Use like this: FocusHelper.releaseFocus(viewToReleaseFocusFrom)

    public class FocusHelper {
        public static void releaseFocus(View view) {
            ViewParent parent = view.getParent();
            ViewGroup group = null;
            View child = null;
            while (parent != null) {
                if (parent instanceof ViewGroup) {
                    group = (ViewGroup) parent;
                    for (int i = 0; i < group.getChildCount(); i++) {
                        child = group.getChildAt(i);
                        if(child != view && child.isFocusable())
                            child.requestFocus();
                    }
                }
                parent = parent.getParent();
            }
        }
    }
    

    Doc: The method traverses from the child view and up the view tree and looks for the first child to give focus to.

    Edit: You can also use the API for this:

    View focusableView = v.focusSearch(View.FOCUS_DOWN);
    if(focusableView != null) focusableView.requestFocus();
    
    0 讨论(0)
  • 2020-12-13 17:34

    new to android.. tried

    getWindow().getDecorView().clearFocus();
    

    it works for me..

    just to add .. your layout should have:

     android:focusable="true"
     android:focusableInTouchMode="true"
    
    0 讨论(0)
  • 2020-12-13 17:36

    Just find another view and give it focus instead.

    var refresher = FindViewById<MvxSwipeRefreshLayout>(Resource.Id.refresher);
    
    refresher.RequestFocus();
    
    0 讨论(0)
  • 2020-12-13 17:41

    Did you try to use old good View.clearFocus()

    0 讨论(0)
提交回复
热议问题