开发者

How to remove auto focus/keyboard popup of a field when the screen shows up?

开发者 https://www.devze.com 2022-12-31 14:01 出处:网络
I have a screen where the first field is an EditText, and it gains the focus at startup, also popups the numeric input type, which is very annoying

I have a screen where the first field is an EditText, and it gains the focus at startup, also popups the numeric input type, which is very annoying

How开发者_如何学Python can I make sure that when the activity is started the focus is not gained, and/or the input panel is not raised?


InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);

or

set activity property in manifest file as below in the application tag

android:windowSoftInputMode="stateHidden"


go to your application manifest file, and write this line for that activity you want to disable auto keyboard pop-up.

android:windowSoftInputMode="stateHidden"


To programatically not have the keyboard displayed, but the default widget still recieve focus call:

getWindow().setSoftInputMode(WindowManager.
                             LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

in onResume()


getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

call the above method inside onCreate().It prevent softKeyboard to show unless user select EditText by tapping or clicking.

or simply add android:windowSoftInputMode="stateHidden" in Activity tag in Manifest.xml


This is usually a mess. The first thing I try is try to steal the focus with another view via . You also have to have the focusable and focusableInTouchMode.

<TextView
  ...
  android:focusable="true"
  android:focusableInTouchMode="true">

    <requestFocus/>
</TextView>


Have another view grab focus. By default, the first focusable View will get focus when a layout is inflated. You can request focus on a different View via XML:

<TextView
    android:layout_width="wrap_parent"
    android:layout_height="wrap_content"
    android:text="Some other view">

    <requestFocus />
</TextView>

This works for any View.

If you want to do it programmatically, you can use view.requestFocus().


Adding android:windowSoftInputMode="stateHidden" to your Activity in manifest only hides the keyboard when you are launching the activity, or as Google says

When the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity

To hide the keyboard also when user presses the back button and moves back to your activity from some other activity, use android:windowSoftInputMode="stateAlwaysHidden"


if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
{
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}


have not tried this nor am i near my programming computer, but I would suspect programmatically sending focus to the parent view or something of that nature could do the trick - thats more likely a workaround than a solution, but again not able to test it just a thought

0

精彩评论

暂无评论...
验证码 换一张
取 消