创建Activity时如何使EditText不聚焦

2022-01-14 00:00:00 focus android java android-edittext

我已经阅读了讨论这个问题的其他问题,所有这些问题都适用于我的布局,除了对于第一个创建的布局.

I've read the other questions discussing this, and all of them work for my layouts, except for the very first one created.

目前,这是我的 onCreate 方法的顶部:

At the moment, this is at the top of my onCreate method:

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

^ 这样至少键盘不会在启动时弹出,但 EditText 仍然专注于.

^ That makes it so at least the keyboard doesn't pop up on startup, but the EditText is still focused on.

这是我的 EditTextXML:

<EditText
    android:id="@+id/password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/changePass"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="167dp"
    android:ems="10"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30" >
</EditText>

这就是我提出我的活动时的样子:

This is what it looks like when I bring up my activity:

问题在于,对于某些手机,当 EditText 像这样聚焦时,它们无法在其中写入.我希望它不聚焦.

The problem is that with some phones, when an EditText is focused like this, they can't write in it. I want it to not focus.

我所做的工作适用于提出的下一个布局,因为 EditTexts 没有关注它,并且看起来更像这样:

What I've done works for the next layouts brought up in that it the EditTexts are not focused on and would look more like this:

但请注意,这是相同的布局.这是用户被带回该屏幕后的屏幕,这表明 XML 没有问题,因为这是相同的 XML,但问题是EditText 仅在创建 Activity 时关注.

Notice that it's the same layout, though. This is the screen after the user has been brought back to this screen, which would indicate nothing wrong with the XML because this is the same XML, but the problem is that the EditText is only focused on when the activity is created.

我已经完成了研究,所有其他问题都没有帮助我解决这个问题(但他们确实帮助键盘没有出现,谢天谢地).我怎样才能使启动时的 EditText 看起来像第二个屏幕截图,而不是第一个屏幕截图?

I've done research and all of the other questions don't help me with this (they did however help the keyboard not show up, thankfully). How can I make it so the EditText on startup will look like the second screenshot, rather than the first?

推荐答案

您可以设置 Layout 的属性,如 android:descendantFocusability="beforeDescendants"android:focusableInTouchMode="true"

You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"

示例:

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

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/changePass"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="167dp"
        android:ems="10"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textPassword"
        android:maxLength="30" >
    </EditText>

</RelativeLayout>

希望这篇文章有帮助;)

May this one helpful ;)

相关文章