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:
bu开发者_开发问答tton.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
精彩评论