如何在创建Activity时让EditText不聚焦

69

我已经阅读了讨论这个问题的其他问题,它们都适用于我的布局,除了第一个创建的布局。

目前,在我的onCreate方法中,这是排在最前面的:

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

这样做至少可以避免键盘在启动时弹出,但是EditText仍然保持焦点。

这是我的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>

这是我打开活动时看到的样子:

enter image description here

问题在于,有些手机在焦点落在EditText上时,无法在其中输入。我希望它不要聚焦。

我所做的对于接下来的布局有效,在这些布局中,EditTexts没有聚焦并且看起来更像这样:

enter image description here

请注意,这是相同的布局。这是用户返回到此屏幕后显示的屏幕,这表明XML没有问题,因为这是相同的XML,但问题在于只有在创建活动时才会聚焦EditText

我已经进行了研究,所有其他问题都无法帮助我解决这个问题(幸运的是,它们确实帮助键盘不出现)。如何使启动时的EditText看起来像第二个屏幕截图,而不是第一个屏幕截图?


2
你尝试过在EditText中使用android:focusable="false"吗? - Pratik Butani
@PratikButani 这样就永远无法聚焦。 - Michael Yaworski
在我的父级布局上添加“android:focusableInTouchMode="true"`对我很有用。 - Marcelo Gracietti
14个回答

301
你可以设置布局的属性,例如 android:descendantFocusability="beforeDescendants"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>

希望这个有用 ;)


19

XML 代码:

<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:focusable="false"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:maxLength="30" >
</EditText>

Java 代码:

EditText edPwd = (EditText)findViewById(R.id.password);
edtPwd.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            v.setFocusable(true);
            v.setFocusableInTouchMode(true);
            return false;
        }
    });
在xml中将焦点设置为false,然后通过代码将其设置为true。

好的解决方案是...将正确答案标记为已解决会导致IME选项中的“actionNext”完全失效。 - Yash Sampat
我先尝试了一下,结果不行。我的错,我在没有触摸屏的模拟器上测试。在真实设备上它运行得很好,谢谢 @Meher。 - Syeda Zunaira

12
在您的主布局中添加以下两行代码:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"> *YOUR LAYOUT CODE* </RelativeLayout>

1
编辑文本并不是焦点,但键盘可见。 - viper

3

最佳解决方案在这里:https://stackoverflow.com/a/45139132/3172843

正确而简单的解决方案是将setFocusable设置为false,将setFocusableInTouchMode设置为true。因此,EditText仅在用户触摸它时获得焦点。

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

2
在onCreate()中添加以下内容。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

在onCreateView()方法中。
 getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

1

XML 代码

<EditText 
    android:imeOptions="flagNoExtractUi"
    android:focusable="false"
    />

在 onClickListerner 中的 Java 代码

 mEdtEnterPhoneNumber.setFocusable(true);
    mEdtEnterPhoneNumber.setFocusableInTouchMode(true);

1
每当我在选择Spinner项目后尝试打开Editext时,此时无法打开键盘并在Edittext中输入值,但我通过以下代码解决了我的问题。

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

<androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="beforeDescendants"
                android:focusableInTouchMode="true">
    
                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/titleName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="textCapWords"
                    android:hint="@string/hint_your_full_name"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

谢谢!


0
最简单的方法是添加


android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 

在 Manifest.xml 文件的 activity 标签中。

0

1) 这是最简单的方法,打开清单文件并在活动标签之间放置以下代码:

android:windowSoftInputMode="stateHidden"

将这些属性放在父布局中。
android:focusable="true" 
android:focusableInTouchMode="true"

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

android:windowSoftInputMode="stateAlwaysHidden"

将这行代码添加到 Manifest.xml 文件中的 activity 标签中。当你点击 EditText 时,键盘会聚焦。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接