简体   繁体   中英

How to set the focus on button?

Hi I have some EditTexts and a button at the bottom of the view. Upon starting the activity, the first EditText will gain focus and a keyboard will pop up. I want to upon starting the activity, the focus will be on the button instead of the EditText and the keyboard should not pop up. I tried running:

button.requestFocus(); 

The EditText still have focus and pops up the keyboard. How can I resolve this? Thanks.

Its trickier than it seems, but certainly possible. See here:

Stop EditText from gaining focus at Activity startup

Put android:windowSoftInputMode="stateHidden" to your Activity which contains EditText

    <activity android:name=".YourActivity" 
        android:screenOrientation="portrait" 
        android:windowSoftInputMode="stateHidden">
    </activity>

You can force hide the keyboard by calling this on the EditText view

    InputMethodManager mgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(myView.getWindowToken(), 0);

Hope this helps,

-serkan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM