【问题标题】:How to make EditText not focused when creating Activity创建Activity时如何使EditText不聚焦
【发布时间】:2013-08-20 04:29:58
【问题描述】:

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

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

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

^ 这使得至少键盘不会在启动时弹出,但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>

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

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

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

但请注意,这是相同的布局。这是用户被带回该屏幕后的屏幕,这表明XML没有问题,因为这是相同的XML,但问题是EditText仅在活动时关注已创建。

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

【问题讨论】:

  • 你在EditText 中尝试过android:focusable="false" 吗?
  • @PratikButani 使它永远无法被关注。
  • 在我的父布局上添加 android:focusableInTouchMode="true" 对我有用。

标签: java android focus android-edittext


【解决方案1】:

您可以设置 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>

希望这篇文章对您有所帮助;)

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 好的解决方案...标记为正确的答案导致 IME 选项 actionNext 完全失败。
    • 我第一次尝试它没有用,我的错误是我在没有触摸屏的模拟器上测试,在真实设备上它的工作就像一个魅力,谢谢@Meher
    【解决方案3】:

    在你的 main_layout 添加这 2 行:

    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>
    

    【讨论】:

    • 编辑文本未聚焦,但键盘可见。
    【解决方案4】:

    在 onCreate() 中添加这个

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

    或在 onCreateView() 中

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

    【讨论】:

      【解决方案5】:

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

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

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

      【讨论】:

        【解决方案6】:

        XML 代码

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

        onClickListerner 中的 Java 代码

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

        【讨论】:

          【解决方案7】:

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

          【讨论】:

          • 这将完全禁用 EditText,这不是我们所需要的。我不确定(因为我没有测试过),但我认为禁用然后重新启用也不会起作用。除非我遗漏了什么,否则请确保您理解问题。
          【解决方案8】:

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

          【讨论】:

            【解决方案9】:

            最简单的方法是添加

            android:windowSoftInputMode="stateAlwaysHidden|adjustPan" 
            

            在 Manifest.xml 文件的活动标签中

            【讨论】:

              【解决方案10】:

              1) 最简单的,打开manifest,把下面的代码放在activity标签之间:

              android:windowSoftInputMode="stateHidden"
              

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

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

              【讨论】:

                【解决方案11】:

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

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

                【讨论】:

                  【解决方案12】:
                  <activity
                              android:name=".MainActivity"
                              android:windowSoftInputMode="stateAlwaysHidden" />
                  

                  android:windowSoftInputMode="stateAlwaysHidden"

                  在 Manifest.xml 文件的活动标记中添加这一行。当您单击 EditText 时,键盘将获得焦点。

                  【讨论】:

                    【解决方案13】:

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

                    【讨论】:

                    • 它可以工作,但这使得它永远无法集中注意力,即使在触摸之后也是如此。
                    • 您可以在需要使其可聚焦时使用事件监听器
                    • 编辑你的答案,我会投票给你。不过,有人已经回答了你所说的内容。
                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-02-12
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-09-08
                    • 2014-06-28
                    • 2014-06-18
                    相关资源
                    最近更新 更多