How to remove focus without setting focus to another control?

后端 未结 11 1757
终归单人心
终归单人心 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:10

    You could try turning off the main Activity's ability to save its state (thus making it forget what control had text and what had focus). You will need to have some other way of remembering what your EditText's have and repopulating them onResume(). Launch your sub-Activities with startActivityForResult() and create an onActivityResult() handler in your main Activity that will update the EditText's correctly. This way you can set the proper button you want focused onResume() at the same time you repopulate the EditText's by using a myButton.post(new Runnable(){ run() { myButton.requestFocus(); } });

    The View.post() method is useful for setting focus initially because that runnable will be executed after the window is created and things settle down, allowing the focus mechanism to function properly by that time. Trying to set focus during onCreate/Start/Resume() usually has issues, I've found.

    Please note this is pseudo-code and non-tested, but it's a possible direction you could try.

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

    What about just adding android:windowSoftInputMode="stateHidden" on your activity in the manifest.

    Taken from a smart man commenting on this: https://stackoverflow.com/a/2059394/956975

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

    First of all, it will 100% work........

    1. Create onResume() method.
    2. Inside this onResume() find the view which is focusing again and again by findViewById().
    3. Inside this onResume() set requestFocus() to this view.
    4. Inside this onResume() set clearFocus to this view.
    5. Go in xml of same layout and find that top view which you want to be focused and set focusable true and focusableInTuch true.
    6. Inside this onResume() find the above top view by findViewById
    7. Inside this onResume() set requestFocus() to this view at the last.
    8. And now enjoy......
    0 讨论(0)
  • 2020-11-28 22:29

    Using clearFocus() didn't seem to be working for me either as you found (saw in comments to another answer), but what worked for me in the end was adding:

    <LinearLayout 
        android:id="@+id/my_layout" 
        android:focusable="true" 
        android:focusableInTouchMode="true" ...>
    

    to my very top level Layout View (a linear layout). To remove focus from all Buttons/EditTexts etc, you can then just do

    LinearLayout myLayout = (LinearLayout) activity.findViewById(R.id.my_layout);
    myLayout.requestFocus();
    

    Requesting focus did nothing unless I set the view to be focusable.

    0 讨论(0)
  • 2020-11-28 22:31
    1. You can use View.clearFocus().

    2. Use View.requestFocus() called from onResume().

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