创建活动时如何使EditText不集中


68

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

目前,这是我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>

这是我进行活动时的样子:

在此处输入图片说明

问题在于,对于某些手机,当EditText像这样聚焦时,他们将无法书写。我希望它不集中精力。

我所做的工作适用于接下来提出的布局,因为EditTexts它们没有重点关注,看起来会更像这样:

在此处输入图片说明

请注意,尽管布局相同。这是将用户带回到该屏幕之后的屏幕,这将表示没什么错,XML因为这是相同的XML,但问题EditText是只是在创建活动时才关注。

我已经进行了研究,所有其他问题对此都无济于事(谢天谢地,它们确实帮助键盘没有出现)。我如何才能使EditText启动时看起来像第二个屏幕截图,而不是第一个?


2
你试过android:focusable="false"了EditText吗?
— Pratik Butani

@PratikButani使得它永远都不能专注于它。
— Michael Yaworski

添加android:focusableInTouchMode="true"我的父母布局对我来说很有效。
— Marcelo Gracietti

Answers:


300

您可以设置布局的财产像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>

希望这有用;)


18

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


好的解决方案...答案标记为正确的结果是IME选项actionNext完全失败。
— 2015年

我首先尝试了它没有用,很糟糕,我在没有触摸屏的模拟器上进行了测试,在真实设备上它的运行就像是一种魅力,谢谢@Meher
— Syeda Zunaira

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>

1
编辑文本未聚焦,但键盘可见。
— 蛇蝎

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

0

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


这将完全禁用EditText,这不是必需的。我不确定(因为我尚未测试),但是我认为禁用然后重新启用也不起作用。除非有我想念的东西,否则请确保您了解问题。
— Michael Yaworski '16

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文件的活动标签中


0

1)最简单的方法是,打开清单并将以下代码放在activity标签之间:

android:windowSoftInputMode="stateHidden"

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

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

0

在清单中,复制并粘贴下面的代码。

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

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

android:windowSoftInputMode =“ stateAlwaysHidden”

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


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

它起作用了,但这使它即使在触摸后也永远无法集中精力。
— Michael Yaworski

您可以在需要使事件聚焦时使用事件侦听器
— Mohammod Hossain 2013年

编辑您的答案以得到答案,我会投票给您。不过,已经有人回答了您所说的话。
— Michael Yaworski 2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.