如何在创建Activity时使EditText不会聚焦

Mic*_*ski 66 java android focus android-edittext

在你下注并标记为重复之前,请阅读我的问题.

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

目前,这是我onCreate方法的首要任务:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

^这使得它至少在启动时没有弹出键盘,但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>
Run Code Online (Sandbox Code Playgroud)

这是我开展活动时的样子:

在此输入图像描述

问题是,对于某些手机,如果EditText像这样聚焦,他们就无法写入.我希望它不要集中注意力.

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

在此输入图像描述

请注意,它的布局是相同的.这是用户返回到此屏幕后的屏幕,这表示没有任何问题,XML因为这是相同的XML,但问题EditText是仅关注创建活动的时间.

我已经完成了研究,所有其他问题对我没有帮助(他们确实帮助键盘不显示,谢天谢地).我怎样才能使它EditText在启动时看起来像第二个截图,而不是第一个?

Pra*_*ani 292

您可以设置布局的属性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>
Run Code Online (Sandbox Code Playgroud)

愿这个有用;)


小智 17

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>
Run Code Online (Sandbox Code Playgroud)

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;
        }
    });
Run Code Online (Sandbox Code Playgroud)

在xml中设置focusable false并通过代码将其设置为true


avi*_*per 12

在你的main_layout中添加以下两行:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)

例:

<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>
Run Code Online (Sandbox Code Playgroud)