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:11
<TextView
    android:id="@+id/TextView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true"
    style="@android:style/Widget.EditText"/>
查看更多
还给你的自由
3楼-- · 2018-12-31 02:12

using the information provided by other posters, I used the following solution:

in the layout XML

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:id="@+id/linearLayout_focus"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_width="0px"
    android:layout_height="0px"/>

<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
    android:id="@+id/autocomplete"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:inputType="textNoSuggestions|textVisiblePassword"/>

in onCreate()

private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    //get references to UI components
    mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}

and finally, in onResume()

@Override
protected void onResume() {
    super.onResume();

    //do not give the editbox focus automatically when activity starts
    mAutoCompleteTextView.clearFocus();
    mLinearLayout.requestFocus();
}
查看更多
伤终究还是伤i
4楼-- · 2018-12-31 02:12

Add android:windowSoftInputMode="stateAlwaysHidden" in the activity tag of the Manifest.xml file.

Source

查看更多
永恒的永恒
5楼-- · 2018-12-31 02:13

Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:

<LinearLayout 'dummy'>
<EditText et>

dummy.setNextFocusDownId(et.getId());

dummy.setNextFocusUpId(et.getId());

et.setNextFocusUpId(et.getId());

a lot of work just to get rid of focus on a view..

Thanks

查看更多
怪性笑人.
6楼-- · 2018-12-31 02:13

You can achieve this by creating a dummy EditText with layout width and height set to 0dp, and request focus to that view. Add the following code snippet in your xml layout:

<EditText
    android:id="@+id/editText0"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:hint="@string/dummy"
    android:ems="10" 
    >
     <requestFocus />
    </EditText>
查看更多
心情的温度
7楼-- · 2018-12-31 02:14

The only solution I've found is:

  • Create a LinearLayout (I dunno if other kinds of Layout's will work)
  • Set the attributes android:focusable="true" and android:focusableInTouchMode="true"

And the EditText won't get the focus after starting the activity

查看更多
登录 后发表回答