停止EditText在活动启动时获得焦点

时间:2023-02-13 18:27:16

I have an Activity in Android, with two elements:

我在Android上有一个活动,有两个元素:

  1. EditText
  2. EditText
  3. ListView
  4. 列表视图

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立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我试着:

EditText.setSelected(false);

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

没有运气。如何说服EditText在活动开始时不选择自己?

46 个解决方案

#1


1987  

Excellent answers from Luc and Mark however a good code sample is missing:

来自Luc和Mark的优秀答案,但是缺少一个好的代码示例:

<!-- 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"/>

#2


1557  

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 EditText having 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> element documentation.

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

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 show it if it isn't already shown, but if it was open when entering the activity, leave it open).

或者android:windowSoftInputMode=" stateconstant " -不要更改(例如,如果还没有显示,不要显示,但如果在进入活动时显示为打开状态,则保持打开状态)。

#3


1004  

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.

现在,当活动开始时,这个主布局将默认得到焦点。

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 comment from 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: descent dantfocusability可用性=“beforeDescendants”似乎是默认值(整数值为0),只需添加android:focusableInTouchMode=“true”即可。

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

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

Thanks to Guillaume.

感谢纪尧姆。

#4


381  

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

而EditText在启动活动后不会得到关注。

#5


67  

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

问题似乎来自一个属性,我只能在布局的XML形式中看到这个属性。

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

确保在EditText XML标记的声明末尾删除这一行:

<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>

#6


62  

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();
}

#7


58  

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中您将focable设置为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,即不使用事件,所以焦点行为将正常进行。

#8


54  

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中的每一个视图都具有可聚焦性和可选择性,我认为您需要明确重点。

#9


43  

I had tried serval answer individually but the focus is still at the EditText. I only manage to solve it by using two of the above solution together.

我曾单独尝试过serval的回答,但重点仍在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://*.com/a/8639921/15695 )

(参考Silver https://*.com/a/8639921/15695)

and remove

和删除

 <requestFocus />

at EditText

在EditText

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

(引用floydaddict https://*.com/a/9681809)

#10


37  

None of this solutions worked for me. The way I fix the autofocus was:

这些解决方案对我都不起作用。我修正自动对焦的方法是:

<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
    <intent-filter >
    </intent-filter>
</activity>

#11


31  

Simple solution: In AndroidManifest in Activity tag use

简单的解决方案:在活动标签中使用android

android:windowSoftInputMode="stateAlwaysHidden"

#12


29  

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...

在布局的第一个TextView中,只需将“focapplicable”和“focable in touch模式”设置为true。这样,当活动开始时,TextView将被集中起来,但是由于它的性质,你将看不到屏幕上的任何东西,当然,也不会显示键盘……

#13


27  

The following worked for me in Manifest. Write ,

以下为我在清单上的工作。写,

<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>

#14


24  

I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.

我需要以编程的方式清除所有字段中的焦点。我只是在主布局定义中添加了以下两条语句。

myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);

That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.

就是这样。解决我的问题。谢谢你,西尔弗,告诉我正确的方向。

#15


24  

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

在清单的活动标记中添加android:windowSoftInputMode="stateAlwaysHidden"。xml文件。

Source

#16


19  

If you have another view on your activity like a ListView, you can also do:

如果你对你的活动有其他的看法,如ListView,你也可以:

ListView.requestFocus(); 

in your onResume() to grab focus from the editText.

在onResume()中,从editText中获取焦点。

I know this question has been answered but just providing an alternative solution that worked for me :)

我知道这个问题已经得到了解答,但只是为我提供了一个替代方案。

#17


17  

Try this before your first editable field:

在第一个可编辑字段之前,请尝试以下操作:

<TextView  
        android:id="@+id/dummyfocus" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/foo"
        />

----

findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();

#18


16  

Add following in onCreate method:

在onCreate方法中添加以下内容:

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

#19


13  

Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!

由于我不喜欢用与功能相关的东西来污染XML,所以我创建了这个方法,它“透明地”从第一个可聚焦的视图中窃取了焦点,然后在必要时确保删除它自己!

public static View preventInitialFocus(final Activity activity)
{
    final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
    final View root = content.getChildAt(0);
    if (root == null) return null;
    final View focusDummy = new View(activity);
    final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View view, boolean b)
        {
            view.setOnFocusChangeListener(null);
            content.removeView(focusDummy);
        }
    };
    focusDummy.setFocusable(true);
    focusDummy.setFocusableInTouchMode(true);
    content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
    if (root instanceof ViewGroup)
    {
        final ViewGroup _root = (ViewGroup)root;
        for (int i = 1, children = _root.getChildCount(); i < children; i++)
        {
            final View child = _root.getChildAt(i);
            if (child.isFocusable() || child.isFocusableInTouchMode())
            {
                child.setOnFocusChangeListener(onFocusChangeListener);
                break;
            }
        }
    }
    else if (root.isFocusable() || root.isFocusableInTouchMode())
        root.setOnFocusChangeListener(onFocusChangeListener);

    return focusDummy;
}

#20


12  

Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus() in onCreate()

晚了,但也许有帮助。在布局的顶部创建一个虚构的EditText,然后在onCreate()中调用myDummyEditText.requestFocus()

<EditText android:id="@+id/dummyEditTextFocus" 
android:layout_width="0px"
android:layout_height="0px" />

That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).

那似乎是我所期望的。不需要处理配置更改等。对于一个带有冗长TextView(指令)的活动,我需要使用这个。

#21


12  

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:

是的,我做了同样的事情——创建一个“假的”线性布局,它得到了初始的焦点。此外,我设置了“next”焦点id,这样用户在滚动一次后就不能再聚焦了:

<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

谢谢

#22


11  

Simplest answer, just add this in parent layout of the XML.

最简单的答案是,在XML的父布局中添加这个。

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

#23


9  

For me, what worked on all devices is this:

对我来说,在所有设备上都起作用的是:

    <!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->

    <View
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />

Just put this as a view before the problematic focused view, and that's it.

把它作为一个视图放在有问题的焦点视图之前,就是这样。

#24


9  

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

这是最完美、最简单的解决方案。我总是在我的应用中使用这个。

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

#25


8  

The simplest thing I did is to set focus on another view in onCreate:

我所做的最简单的事情就是把焦点放在onCreate的另一个视图上:

    myView.setFocusableInTouchMode(true);
    myView.requestFocus();

This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.

这样就不会出现软键盘,EditText中也没有闪烁的光标。

#26


7  

Write this line in your Parent Layout...

把这一行写在你的父布局中……

 android:focusableInTouchMode="true"

#27


6  

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

在活动的onCreate中,只需在EditText元素上添加use clearFocus()。例如,

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,

如果您想将焦点转移到另一个元素上,请使用requestFocus()。例如,

button = (Button) findViewById(R.id.button);
button.requestFocus();

#28


6  

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:

您可以通过创建一个布局宽度和高度设置为0dp的虚拟EditText来实现这一点,并请求将焦点集中到该视图。在xml布局中添加以下代码片段:

<EditText
    android:id="@+id/editText0"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:hint="@string/dummy"
    android:ems="10" 
    >
     <requestFocus />
    </EditText>

#29


6  

Make sure to remove the line "<requestFocus />" from the EditText tag in xml.

确保从xml中的EditText标记中删除“ ”行。

<EditText
   android:id="@+id/input"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">

   <requestFocus /> <!-- remove this line --> 
</EditText>

#30


6  

When your activity is opened, keyboard gets visible automatically which causes focusing of EditText. You can disable keyboard by writing the following line in your activity tag in manifest.xml file.

当您的活动被打开时,键盘会自动显示出来,这将导致EditText的焦点。你可以通过在你的活动标签中写下面一行来禁用键盘。xml文件。

 android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

#1


1987  

Excellent answers from Luc and Mark however a good code sample is missing:

来自Luc和Mark的优秀答案,但是缺少一个好的代码示例:

<!-- 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"/>

#2


1557  

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 EditText having 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> element documentation.

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

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 show it if it isn't already shown, but if it was open when entering the activity, leave it open).

或者android:windowSoftInputMode=" stateconstant " -不要更改(例如,如果还没有显示,不要显示,但如果在进入活动时显示为打开状态,则保持打开状态)。

#3


1004  

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.

现在,当活动开始时,这个主布局将默认得到焦点。

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 comment from 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: descent dantfocusability可用性=“beforeDescendants”似乎是默认值(整数值为0),只需添加android:focusableInTouchMode=“true”即可。

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

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

Thanks to Guillaume.

感谢纪尧姆。

#4


381  

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

而EditText在启动活动后不会得到关注。

#5


67  

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

问题似乎来自一个属性,我只能在布局的XML形式中看到这个属性。

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

确保在EditText XML标记的声明末尾删除这一行:

<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>

#6


62  

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();
}

#7


58  

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中您将focable设置为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,即不使用事件,所以焦点行为将正常进行。

#8


54  

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中的每一个视图都具有可聚焦性和可选择性,我认为您需要明确重点。

#9


43  

I had tried serval answer individually but the focus is still at the EditText. I only manage to solve it by using two of the above solution together.

我曾单独尝试过serval的回答,但重点仍在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://*.com/a/8639921/15695 )

(参考Silver https://*.com/a/8639921/15695)

and remove

和删除

 <requestFocus />

at EditText

在EditText

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

(引用floydaddict https://*.com/a/9681809)

#10


37  

None of this solutions worked for me. The way I fix the autofocus was:

这些解决方案对我都不起作用。我修正自动对焦的方法是:

<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
    <intent-filter >
    </intent-filter>
</activity>

#11


31  

Simple solution: In AndroidManifest in Activity tag use

简单的解决方案:在活动标签中使用android

android:windowSoftInputMode="stateAlwaysHidden"

#12


29  

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...

在布局的第一个TextView中,只需将“focapplicable”和“focable in touch模式”设置为true。这样,当活动开始时,TextView将被集中起来,但是由于它的性质,你将看不到屏幕上的任何东西,当然,也不会显示键盘……

#13


27  

The following worked for me in Manifest. Write ,

以下为我在清单上的工作。写,

<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>

#14


24  

I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.

我需要以编程的方式清除所有字段中的焦点。我只是在主布局定义中添加了以下两条语句。

myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);

That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.

就是这样。解决我的问题。谢谢你,西尔弗,告诉我正确的方向。

#15


24  

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

在清单的活动标记中添加android:windowSoftInputMode="stateAlwaysHidden"。xml文件。

Source

#16


19  

If you have another view on your activity like a ListView, you can also do:

如果你对你的活动有其他的看法,如ListView,你也可以:

ListView.requestFocus(); 

in your onResume() to grab focus from the editText.

在onResume()中,从editText中获取焦点。

I know this question has been answered but just providing an alternative solution that worked for me :)

我知道这个问题已经得到了解答,但只是为我提供了一个替代方案。

#17


17  

Try this before your first editable field:

在第一个可编辑字段之前,请尝试以下操作:

<TextView  
        android:id="@+id/dummyfocus" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/foo"
        />

----

findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();

#18


16  

Add following in onCreate method:

在onCreate方法中添加以下内容:

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

#19


13  

Being that I don't like to pollute the XML with something that is related to functionality, I created this method that "transparently" steals the focus from the first focusable view and then makes sure to remove itself when necessary!

由于我不喜欢用与功能相关的东西来污染XML,所以我创建了这个方法,它“透明地”从第一个可聚焦的视图中窃取了焦点,然后在必要时确保删除它自己!

public static View preventInitialFocus(final Activity activity)
{
    final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
    final View root = content.getChildAt(0);
    if (root == null) return null;
    final View focusDummy = new View(activity);
    final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View view, boolean b)
        {
            view.setOnFocusChangeListener(null);
            content.removeView(focusDummy);
        }
    };
    focusDummy.setFocusable(true);
    focusDummy.setFocusableInTouchMode(true);
    content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
    if (root instanceof ViewGroup)
    {
        final ViewGroup _root = (ViewGroup)root;
        for (int i = 1, children = _root.getChildCount(); i < children; i++)
        {
            final View child = _root.getChildAt(i);
            if (child.isFocusable() || child.isFocusableInTouchMode())
            {
                child.setOnFocusChangeListener(onFocusChangeListener);
                break;
            }
        }
    }
    else if (root.isFocusable() || root.isFocusableInTouchMode())
        root.setOnFocusChangeListener(onFocusChangeListener);

    return focusDummy;
}

#20


12  

Late, but maybe helpful. Create a dummy EditText at the top of your layout then call myDummyEditText.requestFocus() in onCreate()

晚了,但也许有帮助。在布局的顶部创建一个虚构的EditText,然后在onCreate()中调用myDummyEditText.requestFocus()

<EditText android:id="@+id/dummyEditTextFocus" 
android:layout_width="0px"
android:layout_height="0px" />

That seems to behave as I expect. No need to handle configuration changes, etc. I needed this for an Activity with a lengthy TextView (instructions).

那似乎是我所期望的。不需要处理配置更改等。对于一个带有冗长TextView(指令)的活动,我需要使用这个。

#21


12  

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:

是的,我做了同样的事情——创建一个“假的”线性布局,它得到了初始的焦点。此外,我设置了“next”焦点id,这样用户在滚动一次后就不能再聚焦了:

<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

谢谢

#22


11  

Simplest answer, just add this in parent layout of the XML.

最简单的答案是,在XML的父布局中添加这个。

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

#23


9  

For me, what worked on all devices is this:

对我来说,在所有设备上都起作用的是:

    <!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->

    <View
    android:layout_width="0px"
    android:layout_height="0px"
    android:focusable="true"
    android:focusableInTouchMode="true" />

Just put this as a view before the problematic focused view, and that's it.

把它作为一个视图放在有问题的焦点视图之前,就是这样。

#24


9  

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

这是最完美、最简单的解决方案。我总是在我的应用中使用这个。

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

#25


8  

The simplest thing I did is to set focus on another view in onCreate:

我所做的最简单的事情就是把焦点放在onCreate的另一个视图上:

    myView.setFocusableInTouchMode(true);
    myView.requestFocus();

This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.

这样就不会出现软键盘,EditText中也没有闪烁的光标。

#26


7  

Write this line in your Parent Layout...

把这一行写在你的父布局中……

 android:focusableInTouchMode="true"

#27


6  

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

在活动的onCreate中,只需在EditText元素上添加use clearFocus()。例如,

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,

如果您想将焦点转移到另一个元素上,请使用requestFocus()。例如,

button = (Button) findViewById(R.id.button);
button.requestFocus();

#28


6  

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:

您可以通过创建一个布局宽度和高度设置为0dp的虚拟EditText来实现这一点,并请求将焦点集中到该视图。在xml布局中添加以下代码片段:

<EditText
    android:id="@+id/editText0"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:hint="@string/dummy"
    android:ems="10" 
    >
     <requestFocus />
    </EditText>

#29


6  

Make sure to remove the line "<requestFocus />" from the EditText tag in xml.

确保从xml中的EditText标记中删除“ ”行。

<EditText
   android:id="@+id/input"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">

   <requestFocus /> <!-- remove this line --> 
</EditText>

#30


6  

When your activity is opened, keyboard gets visible automatically which causes focusing of EditText. You can disable keyboard by writing the following line in your activity tag in manifest.xml file.

当您的活动被打开时,键盘会自动显示出来,这将导致EditText的焦点。你可以通过在你的活动标签中写下面一行来禁用键盘。xml文件。

 android:windowSoftInputMode="stateAlwaysHidden|adjustPan"