禁用编辑文本的自动对焦

Saa*_*aad 58 android autofocus android-layout android-edittext

我有一个编辑文本:

   <LinearLayout android:id="@+id/linearLayout7" android:layout_width="match_parent" android:layout_height="wrap_content">
        <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/editText1" android:text="3">
            <requestFocus></requestFocus>
        </EditText>
        <Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button2"></Button>
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后是其他一些东西

我遇到的问题是,当应用程序启动时焦点在输入上,我不希望在应用程序启动时焦点在输入上.

我试过删除

<requestFocus></requestFocus>
Run Code Online (Sandbox Code Playgroud)

事情,它没有做任何事情.

Mud*_*sir 135

EditText的父布局中添加android:focusable="true"android:focusableInTouchMode="true"元素如下;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout7" android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:focusable="true" android:focusableInTouchMode="true">
Run Code Online (Sandbox Code Playgroud)

我想,它应该对你有帮助.

  • 如果它不起作用,也将它添加到父级:`android:descendantFocusability ="beforeDescendants"` (3认同)
  • @Saad:有效。在将其发布到这里之前,我已经对其进行了测试。将元素保留在XML文件的顶部。顺便说一句,您正在开发哪个API级别(或Android版本)? (2认同)

Nis*_*abu 10

没有更多的工作...只需将android:windowSoftInputMode ="stateAlwaysHidden"添加到Manifest.xml文件的活动标签.

  • 这将隐藏键盘,但如果我没记错的话,EditText仍保持焦点. (4认同)
  • 请解释它的作用以及它如何帮助 OP。即使修复可以奏效,没有解释的建议也无济于事。 (2认同)
  • API 27 提到的所有方法都不起作用! (2认同)

小智 7

如果您想清除默认的编辑文本焦点,请使用以下2个属性

android:focusable="false"
 android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)

在父级线性布局中。

例如:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="true"
            android:orientation="vertical">


     <EditText
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="email id"
         android:inputType="textEmailAddress"
         android:maxLines="1"
    />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

如果要在创建活动时隐藏键盘,请在清单文件活动标记中使用以下属性,例如:

 <activity
     android:configChanges="screenSize|orientation|keyboardHidden"
     android:screenOrientation="portrait"
     android:name=".activities.LoginActivity"
     android:windowSoftInputMode="stateHidden|adjustResize"/>
Run Code Online (Sandbox Code Playgroud)

如果要在单击按钮或发生某些事件时隐藏键盘,请使用以下代码

 public void onClick(View v) {
                try {
                    //InputMethodManager is used to hide the virtual keyboard from the user after finishing the user input
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm.isAcceptingText()) {
                        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                    }
                } catch (NullPointerException e) {
                    Log.e("Exception", e.getMessage() + ">>");
                }
                    }
                } catch (NullPointerException e) {
                    Log.e("Exception", e.getMessage() + ">>");
                }
Run Code Online (Sandbox Code Playgroud)

如果要在离开活动后从编辑文本字段中移出焦点

@Override
    protected void onResume() {
        super.onResume();
        mEmail.clearFocus();
        mPassword.clearFocus();
}
Run Code Online (Sandbox Code Playgroud)

最后,如果您想在提交表单时清除编辑文本字段中的数据,请使用

 @Override
    protected void onResume() {
        super.onResume();
        mEmail.getText().clear();
        mPassword.getText().clear();
    }
Run Code Online (Sandbox Code Playgroud)


小智 5

<LinearLayout 
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:layout_width="0px"
    android:layout_height="0px" />

 <EditText android:text="" 
    android:id="@+id/EditText01"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:hint="@string/hint">
 </EditText>
Run Code Online (Sandbox Code Playgroud)

无需其他编码,这是一个简单明了的解决方案。