84

我有一个编辑文本:

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

然后下面还有一些其他的东西

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

我尝试删除

<requestFocus></requestFocus>

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

4

12 回答 12

177

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

我想,它应该对你有所帮助。

也可以看看;
    Android 开发者官方处理触摸和输入指南

于 2011-09-29T08:34:11.083 回答
20

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

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

在父线性布局内。

例如:

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

如果您想在创建活动时隐藏键盘,请使用清单文件活动标签中的这些属性,例如:

 <activity
     android:configChanges="screenSize|orientation|keyboardHidden"
     android:screenOrientation="portrait"
     android:name=".activities.LoginActivity"
     android:windowSoftInputMode="stateHidden|adjustResize"/>

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

 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() + ">>");
                }

如果您想在离开活动后从编辑文本字段中取消焦点

@Override
    protected void onResume() {
        super.onResume();
        mEmail.clearFocus();
        mPassword.clearFocus();
}

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

 @Override
    protected void onResume() {
        super.onResume();
        mEmail.getText().clear();
        mPassword.getText().clear();
    }
于 2018-01-05T12:01:15.707 回答
10

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

于 2016-08-10T13:55:35.293 回答
9
<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>

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

于 2017-08-24T09:23:01.930 回答
2

您可以使用隐藏的窗口软输入模式隐藏键盘android:focusable="false"android:focusableInTouchMode="true"

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="stateHidden"
    android:label="@string/app_name"
    >

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
于 2018-02-07T05:15:05.427 回答
2

在 LinearLayout 中尝试此代码

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

        <EditText

            android:hint="Search..."
            android:drawableRight="@drawable/ic_search"
            android:id="@+id/search_tab_hotbird_F"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyHotbird_F"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="4dp">

        </android.support.v7.widget.RecyclerView>

    </LinearLayout>
于 2019-02-04T14:30:28.887 回答
2

它对我有用,希望也能帮助你。

任何一个 :

android:windowSoftInputMode="stateAlwaysHidden"

将此属性添加到要禁用自动对焦的特定活动下的 Manifest.xml 文件中。 缺点:它只会隐藏你的软键盘,光标还会在那里。

别的 :

 android:focusableInTouchMode="true"

将此属性添加到您的 UI xml 文件(在父布局下),以便它同时隐藏键盘焦点。

于 2019-04-01T11:25:45.370 回答
1
<EditText
        android:id="@+id/input"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

detailInputView = (EditText) debugToolsView.findViewById(R.id.input);
    detailInputView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            detailInputView.setFocusable(true);
            detailInputView.setFocusableInTouchMode(true);
            return false;
        }
    });
于 2016-10-14T03:47:06.200 回答
1

只需将以下内容添加到您的主布局视图 xml 标记中:

 android:focusableInTouchMode="true"
于 2017-01-26T10:37:14.177 回答
1

有几种方法可以做到这一点,Mudassir给了你一个方法来做到这一点。

如果这个想法行不通,那就做简单的事情——

在你AndroidManifest>Activity添加这个简单的代码:

android:windowSoftInputMode="stateHidden"

在 XML 中的 EditText 中不需要做任何事情,一切都会工作并且不会出现焦点。

看看现实生活中的示例代码:

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="stateHidden"
        android:label="@string/app_name"
        >

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
于 2017-06-07T08:35:54.790 回答
0

为了明确的默认 requestFocuse 你应该像下面这样设置你的视图的父 focusable="true"

<android.support.constraint.ConstraintLayout
                android:id="@+id/coordinatorChild"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="true"
                android:focusableInTouchMode="true">
于 2019-06-19T10:25:19.333 回答
0

在我使父视图可见之前,我这样做(并且工作正常)编辑文本不会在我触摸它时自动聚焦。(parentView 包括编辑文本)

... 
parentView.setFocusable(true);
parentView.setFocusableInTouchMode(true);
objProperties.setVisibility(VISIBLE);
...
于 2019-11-02T16:43:47.480 回答