如何阻止 EditText 在 Android 的 Activity 启动时获得焦点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1555109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:15:09  来源:igfitidea点击:

How to stop EditText from gaining focus at Activity startup in Android

androidlistviewandroid-edittextfocus

提问by Mark

I have an Activityin Android, with two elements:

Activity在 Android 中有一个,有两个元素:

  1. EditText
  2. ListView
  1. EditText
  2. ListView

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

当我Activity开始时,EditText立即有输入焦点(闪烁的光标)。我不希望任何控件在启动时具有输入焦点。我试过:

EditText.setSelected(false);
EditText.setFocusable(false);

No luck. How can I convince the EditTextto not select itself when the Activitystarts?

没运气。我怎样才能说服他们EditTextActivity开始时不要选择自己?

采纳答案by Morgan Christiansson

Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true"and android:focusable="true"to parent layout (e.g. LinearLayoutor ConstraintLayout) like the following example will fix the problem.

Luc 和 Mark 给出了很好的答案,但是缺少一个好的代码示例。像下面的示例一样将标签android:focusableInTouchMode="true"和添加android:focusable="true"到父布局(例如LinearLayoutConstraintLayout)将解决该问题。

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

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext" 
    android:nextFocusLeft="@id/autotext"/>

回答by Joe

Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText? I don't really see an issue with the EditTexthaving focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText(and open the keyboard as a result).

实际问题是您根本不希望它具有焦点吗?或者您不希望它因为聚焦而显示虚拟键盘EditText?我并没有真正看到EditText关注开始的问题,但是当用户没有明确要求关注EditText(并因此打开键盘)时,打开 softInput 窗口绝对是一个问题。

If it's the problem of the virtual keyboard, see the AndroidManifest.xml<activity> elementdocumentation.

如果是虚拟键盘的问题,请参阅AndroidManifest.xml<activity> 元素文档。

android:windowSoftInputMode="stateHidden"- always hide it when entering the activity.

android:windowSoftInputMode="stateHidden"- 进入活动时始终隐藏它。

or android:windowSoftInputMode="stateUnchanged"- don't change it (e.g. don't showit if it isn't already shown, but if it was open when entering the activity, leave it open).

android:windowSoftInputMode="stateUnchanged"- 不要更改它(例如,如果它尚未显示,则不要显示它,但如果它在进入活动时已打开,则将其保持打开状态)。

回答by Silver

A simpler solution exists. Set these attributes in your parent layout:

存在更简单的解决方案。在父布局中设置这些属性:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true" >

And now, when the activity starts this main layout will get focus by default.

现在,当 Activity 开始时,这个主布局将默认获得焦点。

Also, we can remove focus from child views at runtime (e.g., after finishing child editing) by giving the focus to the main layout again, like this:

此外,我们可以在运行时(例如,完成子编辑后)通过再次将焦点移至主布局来从子视图中移除焦点,如下所示:

findViewById(R.id.mainLayout).requestFocus();

Good commentfrom Guillaume Perrot:

纪尧姆·佩罗( Guillaume Perrot) 的好评

android:descendantFocusability="beforeDescendants"seems to be the default (integer value is 0). It works just by adding android:focusableInTouchMode="true".

android:descendantFocusability="beforeDescendants"似乎是默认值(整数值为 0)。它只需添加 android:focusableInTouchMode="true".

Really, we can see that the beforeDescendantsset as default in the ViewGroup.initViewGroup()method (Android 2.2.2). But not equal to 0. ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;

真的,我们可以看到beforeDescendantsViewGroup.initViewGroup()方法中设置为默认值(Android 2.2.2)。但不等于0。ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;

Thanks to Guillaume.

感谢纪尧姆。

回答by Luc

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"
  • 创建一个LinearLayout(我不知道其他类型的 Layout 是否可以工作)
  • 设置属性android:focusable="true"android:focusableInTouchMode="true"

And the EditTextwon't get the focus after starting the activity

并且EditText在开始活动后不会获得焦点

回答by floydaddict

The problem seems to come from a property that I can only see in the XML formof the layout.

问题似乎来自我只能在XML form布局中看到的属性。

Make sure to remove this line at the end of the declaration within the EditTextXML tags:

确保在EditTextXML 标记内的声明末尾删除此行:

<requestFocus />

That should give something like that :

那应该给出类似的东西:

<EditText
   android:id="@+id/emailField"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:inputType="textEmailAddress">

   //<requestFocus /> /* <-- without this line */
</EditText>

回答by Someone Somewhere

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

使用其他海报提供的信息,我使用了以下解决方案:

in the layout XML

在布局 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()

在 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()

最后,在 onResume()

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

    //do not give the editbox focus automatically when activity starts
    mAutoCompleteTextView.clearFocus();
    mLinearLayout.requestFocus();
}

回答by Eric Mill

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.

尝试clearFocus()而不是setSelected(false)。Android 中的每个视图都有可聚焦性和可选择性,我认为您只想清除焦点。

回答by MinceMan

The following will stop edittext from taking focus when created, but grab it when you touch them.

以下将阻止 edittext 在创建时获得焦点,但在您触摸它们时抓住它。

<EditText
    android:id="@+id/et_bonus_custom"
    android:focusable="false" />

So you set focusable to false in the xml, but the key is in the java, which you add the following listener:

因此,您在 xml 中将 focusable 设置为 false,但关键是在 java 中,您在其中添加了以下侦听器:

etBonus.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.setFocusable(true);
        v.setFocusableInTouchMode(true);
        return false;
    }
});

Because you are returning false, i.e. not consuming the event, the focusing behavior will proceed like normal.

因为您返回 false,即不消耗该事件,所以聚焦行为将像往常一样进行。

回答by Lee Yi Hong

I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.

我已经分别尝试了几个答案,但重点仍然是 EditText。我只能通过同时使用以下两个解决方案来解决它。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainLayout"
  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true" >

( Reference from Silver https://stackoverflow.com/a/8639921/15695)

(引用自 Silver https://stackoverflow.com/a/8639921/15695

and remove

并删除

 <requestFocus />

at EditText

在编辑文本

( Reference from floydaddict https://stackoverflow.com/a/9681809)

(来自 floydacci 的参考https://stackoverflow.com/a/9681809

回答by Rishabh Saxena

Late but simplest answer, just add this in parent layout of the XML.

迟到但最简单的答案,只需将其添加到 XML 的父布局中即可。

android:focusable="true" 
android:focusableInTouchMode="true"

Upvote if it helped you ! Happy Coding :)

对你有帮助就点个赞吧!快乐编码:)