禁用编辑文本的自动对焦

[英]Disable auto focus on edit text


I have an edit text:

我有一个编辑文本:

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

and then some other stuff underneath

然后是其他一些东西

The problem I'm having is that right when the app starts the focus is on the input, i dont want focus to be on input right when the app starts.

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

I tried removing the

我试过删除

<requestFocus></requestFocus>

thing and it didn't do anything.

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

8 个解决方案

#1


96  

Add the android:focusable="true" and android:focusableInTouchMode="true" elements in the parent layout of EditText as follow;

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

I think, it should help you.

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

#2


3  

NO More work... just add android:windowSoftInputMode="stateAlwaysHidden" to activity tag of the Manifest.xml file .

没有更多工作......只需将android:windowSoftInputMode =“stateAlwaysHidden”添加到Manifest.xml文件的activity标签即可。

#3


2  

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

No other coding needed, it's a simple and clear solution.

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

#4


2  

If you want clear the default focus on the edit text then use the following 2 attributes

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

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

inside the parent Linear layout.

在父线性布局中。

for example:

例如:

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

If you want to hide the keyboard on activity creation use these attributes inside manifest file activity tag for example:

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

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

If you want to hide the keyboard on button click or on some event occur use the following code

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

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

If you want to take off the focus from edit text fields after leaving the activity

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

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

And finally If you want to clear the data in the edit text fields on submitting the form use

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

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

#5


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

#6


1  

You can use window soft input mode hidden for hide keyboard and android:focusable="false" and android:focusableInTouchMode="true"

您可以使用隐藏键盘和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>

#7


0  

Just add the following to your main layout view xml tag:

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

 android:focusableInTouchMode="true"

#8


0  

Hei Lee,

黑李,

There are several way to do this, Mudassir has give a way to you to do that.

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

If the idea doesn't work, then do just simple thing-

如果这个想法不起作用,那就做一件简单的事 -

In your AndroidManifest>Activity add this simple code:

在AndroidManifest> Activity中添加以下简单代码:

android:windowSoftInputMode="stateHidden"

Nothing need to do in EditText in XML, all will be work and no focus will appear.

在XML中的EditText中没有什么需要做的,一切都会起作用,不会出现焦点。

Look the real life example code:

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

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/09/29/fa81b88250871a0328ef96bd194c9154.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号