Stop EditText from gaining focus at Activity start

2018-12-31 01:33发布

I have an Activity in Android, with two elements:

  1. EditText
  2. ListView

When my Activity starts, the EditText immediately has input focus (flashing cursor). I don't want any control to have input focus at startup. I tried:

EditText.setSelected(false);

No luck. How can I convince the EditText to not select itself when the Activity starts?

30条回答
人间绝色
2楼-- · 2018-12-31 02:05

The following worked for me in Manifest. Write ,

<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
查看更多
姐姐魅力值爆表
3楼-- · 2018-12-31 02:07

At onCreate of your Activity, just add use clearFocus() on your EditText element. For example,

edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();

And if you want to divert the focus to another element, use requestFocus() on that. For example,

button = (Button) findViewById(R.id.button);
button.requestFocus();
查看更多
看风景的人
4楼-- · 2018-12-31 02:09

This is the perfect and most easiest solution.I always use this in my app.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
查看更多
回忆,回不去的记忆
5楼-- · 2018-12-31 02:11

Try clearFocus() instead of setSelected(false). Every view in Android has both focusability and selectability, and I think that you want to just clear the focus.

查看更多
情到深处是孤独
6楼-- · 2018-12-31 02:11

Simple solution: In AndroidManifest in Activity tag use

android:windowSoftInputMode="stateAlwaysHidden"
查看更多
看淡一切
7楼-- · 2018-12-31 02:11

You can just set "focusable" and "focusable in touch mode" to value true on the first TextView of the layout. In this way when the activity starts the TextView will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...

查看更多
登录 后发表回答