如何让EditText创建Activity时不聚焦

问题描述 投票:0回答:15

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

目前,这是我的

onCreate
方法的顶部:

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

^ 这使得至少键盘在启动时不会弹出,但

EditText
仍然受到关注。

这是我的

XML
EditText

<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
仅在 Activity 时聚焦已创建。

我已经做了研究,所有其他问题都对我没有帮助(但幸运的是,它们确实帮助键盘不显示)。我怎样才能使启动时的

EditText
看起来像第二个屏幕截图,而不是第一个?

java android focus android-edittext
15个回答
302
投票

您可以设置 Layout 的属性,如

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 中设置 focusable false 并通过代码将其设置为 true


12
投票

在你的 main_layout 中 添加这两行:

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>

3
投票

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

正确且简单的解决方案是 setFocusable false 和 setFocusableInTouchMode true 。因此,只有当用户触摸该 EditText 时,EditText 才会获得焦点 android:focusable="false" android:focusableInTouchMode="true"



2
投票

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

或者在onCreateView()中

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



1
投票

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

onClickListerner 中的 Java 代码

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



1
投票

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
投票
editText.setEnabled(false)

方法中使用

onStart()
(而不是在 onCreate() 中 - 因为这是初始化 GUI 组件的一些方法)
    


0
投票
android:focusable="false"

来禁用它,但如果由于某种原因这不起作用,那么您可以简单地放置

LinearLayout
,它将获得焦点,而不会破坏您的布局。

注意:Eclipse 会给您一个错误,指出您的

LinearLayout

无用,因为它没有内容。您应该能够毫无问题地忽略它。


例如:

<LinearLayout android:focusable="true" android:focusableInTouchMode="false" android:layout_width="0dp" android:layout_height="0dp" /> <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> </LinearLayout>



0
投票

android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

在Manifest.xml文件的activity标签中


0
投票

android:windowSoftInputMode="stateHidden"

2)将此属性放入父布局中

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



0
投票
在 Manifest 中,复制并粘贴以下代码。

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



0
投票
  
android:windowSoftInputMode="stateAlwaysHidden"

将此行添加到 Manifest.xml 文件的活动标记中。单击 EditText 时,键盘将成为焦点。


0
投票


-7
投票
android:focusable="false"

禁用对焦


<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" android:focusable="false" > </EditText>

© www.soinside.com 2019 - 2024. All rights reserved.