Java 创建 Activity 时如何使 EditText 不聚焦

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18295802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:11:46  来源:igfitidea点击:

How to make EditText not focused when creating Activity

javaandroidfocusandroid-edittext

提问by Michael Yaworski

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

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

At the moment, this is at the top of my onCreatemethod:

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

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

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

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

This is the XMLfor my 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>

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

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

enter image description here

在此处输入图片说明

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

问题是,对于某些手机,当EditText像这样聚焦时,他们无法写入。我希望它聚焦。

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

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

enter image description here

在此处输入图片说明

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 XMLbecause this is the same XML, but the problem is that the EditTextis only focused on when the activity is created.

但是请注意,它是相同的布局。这是用户被带回此屏幕后的屏幕,这表明 没有任何问题,XML因为这是相同的XML,但问题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 EditTexton startup will look like the second screenshot, rather than the first?

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

采纳答案by Pratik Butani

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

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

Example:

例子:

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

愿这有帮助;)

回答by Mohammod Hossain

use android:focusable="false"for focusing disable

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

回答by Meher

XML code:

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

爪哇代码:

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

set focusable false in xml and set it true via the code

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

回答by ThatGuyThere

It is possible to use android:focusable="false"to disable it, but if that does not work for some reason, then you can simply put a LinearLayoutand it will take the focus without disrupting your layout.

可以使用android:focusable="false"来禁用它,但是如果由于某种原因这不起作用,那么您可以简单地放置 aLinearLayout并且它将在不中断您的布局的情况下获得焦点。

NOTE: Eclipse will give you an error saying that your LinearLayoutis useless because it has no contents. You should be able to disregard it with no problems.

注意:Eclipse 会给你一个错误,说你LinearLayout没用,因为它没有内容。您应该可以毫无问题地忽略它。

For example:

例如:

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

回答by Nicole Conrad

You can do this programmatically. use editText.setEnabled(false)in your onStart()method of your activity (not in onCreate() - because this is some method for initializing GUI components)

您可以以编程方式执行此操作。editText.setEnabled(false)在您onStart()的活动方法中使用(不在 onCreate() 中 - 因为这是初始化 GUI 组件的一些方法)

回答by avisper

In your main_layout add this 2 lines:

在您的 main_layout 添加这 2 行:

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

example:

例子:

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

回答by Hai Rom

XML Code

XML 代码

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

Java Code in onClickListerner

onClickListerner 中的 Java 代码

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

回答by Sanket Parchande

Add this in onCreate()

在 onCreate() 中添加这个

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

or in onCreateView()

或在 onCreateView()

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

回答by faraz khonsari

the best solution is here: https://stackoverflow.com/a/45139132/3172843

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

the correct and simplesolution is to setFocusable false and setFocusableInTouchMode true. so the EditText gain focus only when user touch that EditText

正确的和简单的解决方案是setFocusable虚假和setFocusableInTouchMode真。所以只有当用户触摸 EditText 时 EditText 才会获得焦点

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

回答by pruthwiraj.kadam

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

android:windowSoftInputMode="stateAlwaysHidden"

android:windowSoftInputMode="stateAlwaysHidden"

add this line in the activity tag of the Manifest.xml file.when you click on the EditText the keyboard gets into focus.

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