简体   繁体   English

创建Activity时如何使EditText不聚焦

[英]How to make EditText not focused when creating Activity

I've read the other questions discussing this, and all of them work for my layouts, except for the very first one created.我已经阅读了讨论这个问题的其他问题,除了第一个创建的布局之外,所有这些问题都适用于我的布局。

At the moment, this is at the top of my onCreate method:目前,这是我的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 EditText is still focused on. ^ 这使得至少键盘不会在启动时弹出,但EditText仍然专注于。

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

This is what it looks like when I bring up my activity:这是我提出活动时的样子:

在此处输入图像描述

The problem is that with some phones, when an EditText is focused like this, they can't write in it.问题在于,对于某些手机,当EditText像这样聚焦时,它们无法在其中写入。 I want it to not focus.我希望它集中。

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:我所做的工作适用于下一个布局,因为EditTexts没有关注它,并且看起来更像这样:

在此处输入图像描述

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.这是用户返回此屏幕后的屏幕,这表明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 EditText on startup will look like the second screenshot, rather than the first?我怎样才能使启动时的EditText看起来像第二个屏幕截图,而不是第一个屏幕截图?

You can set property of Layout like android:descendantFocusability="beforeDescendants" and android:focusableInTouchMode="true"您可以设置布局的属性,如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 ;)愿这有帮助;)

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

set focusable false in xml and set it true via the code在 xml 中设置可聚焦的 false 并通过代码将其设置为 true

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>

the best solution is here: https://stackoverflow.com/a/45139132/3172843最好的解决方案在这里: https ://stackoverflow.com/a/45139132/3172843

the correct and simple solution is to setFocusable false and setFocusableInTouchMode true .正确而简单的解决方案是 setFocusable false 和setFocusableInTouchMode true so the EditText gain focus only when user touch that EditText所以只有当用户触摸 EditText 时 EditText 才会获得焦点

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

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

XML Code XML 代码

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

Java Code in onClickListerner onClickListerner 中的 Java 代码

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

Whenever I'm trying to open Editext after selection item of Spinner, so on that time not able to open keyboard & write down the values in Edit text, but I resolved my issue with this code.每当我在 Spinner 的选择项之后尝试打开 Editext 时,所以当时无法打开键盘并写下 Edit 文本中的值,但我用这段代码解决了我的问题。

android:descendantFocusability="beforeDescendants" android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"安卓: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>

Thanks!谢谢!

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 LinearLayout and it will take the focus without disrupting your layout.可以使用android:focusable="false"来禁用它,但如果由于某种原因这不起作用,那么您可以简单地放置一个LinearLayout ,它会在不破坏您的布局的情况下获得焦点。

NOTE: Eclipse will give you an error saying that your LinearLayout is useless because it has no contents.注意:Eclipse 会给你一个错误,说你的LinearLayout是无用的,因为它没有内容。 You should be able to disregard it with no problems.您应该可以毫无问题地忽略它。

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>

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)在您的活动的onStart()方法中使用editText.setEnabled(false) (而不是在 onCreate() - 因为这是一些用于初始化 GUI 组件的方法)

<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 时,键盘将成为焦点。

The easiest way is to add最简单的方法是添加

android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 

in the activity tag of the Manifest.xml file在 Manifest.xml 文件的活动标记中

1) Its the simplest,open the manifest and put the following code between the activity tag: 1)它最简单,打开清单并将以下代码放在活动标签之间:

android:windowSoftInputMode="stateHidden"

2) Put this attributes in the parent layout 2)将此属性放在父布局中

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

In Manifest , copy and paste the beneath code.在 Manifest 中,复制并粘贴下面的代码。

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

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在焦点 EditText 之外单击时隐藏软键盘,但如果单击另一个 EditText 则不隐藏? - How to make the soft keyboard hide when clicked outside of the focused EditText BUT not if another EditText is clicked? 焦点时如何将EditText移到顶部? - android - How to shift EditText to top when focused? 聚焦EditText时如何隐藏IME - How to hide IME when EditText is focused 聚焦edittext时如何显示软键盘 - How to show soft-keyboard when edittext is focused 聚焦editText并打开键盘时如何移动视图? - How to move view when editText is focused and keyboard is open? 如何使活动A中的edittext值出现在活动B中的列表视图中? - How to make edittext value in activity A appear in a listview in activity B? Android:在聚焦EditText时不调用onBackPressed - Android: onBackPressed is not called when EditText is focused 创建新的Activity时,为什么需要将EditText对象转换为EditText对象? - When creating a new Activity why do we need to cast an EditText object as an EditText object? 选中单选按钮时如何在另一个活动中显示EditText输入? - How to display EditText input in another activity when Radio Button is checked? 从一种活动转移到另一种活动时,如何保持editText输入? - How to keep editText inputs when moving from one activity to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM