如何在edittext上禁用键盘弹出?

Hou*_*fly 81 android android-edittext

在我的应用程序中,我所有屏幕的第一个视图是EditText,因此每次进入屏幕时,屏幕键盘都会弹出.如何禁用此弹出窗口并在手动单击EditText时启用它?

    eT = (EditText) findViewById(R.id.searchAutoCompleteTextView_feed);

    eT.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {

            if(hasFocus){
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
            imm.hideSoftInputFromWindow(eT.getWindowToken(), 0);
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

xml代码:

<ImageView
    android:id="@+id/feedPageLogo"
    android:layout_width="45dp"
    android:layout_height="45dp"
    android:src="@drawable/wic_logo_small" />

<Button
    android:id="@+id/goButton_feed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="@string/go" />

<EditText
    android:id="@+id/searchAutoCompleteTextView_feed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/goButton_feed"
    android:layout_toRightOf="@id/feedPageLogo"
    android:hint="@string/search" />

<TextView
    android:id="@+id/feedLabel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/feedPageLogo"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/feed"
    android:textColor="@color/white" />

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ButtonsLayout_feed"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/feedButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/feed"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/iWantButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/iwant"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/shareButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/share"
        android:textColor="@color/black" />

    <Button
        android:id="@+id/profileButton_feed"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_margin="0dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:text="@string/profile"
        android:textColor="@color/black" />
</LinearLayout>

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/feedListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/ButtonsLayout_feed"
    android:layout_below="@id/feedLabel"
    android:textSize="15dp" >
</ListView>
Run Code Online (Sandbox Code Playgroud)

第三个视图(EditText)是焦点所在的位置.

Sky*_*net 167

最佳解决方案位于Project Manifest文件(AndroidManifest.xml)中,在activity构造中添加以下属性

机器人:windowSoftInputMode = "stateHidden"


例:

    <activity android:name=".MainActivity" 
              android:windowSoftInputMode="stateHidden" />
Run Code Online (Sandbox Code Playgroud)

描述:

  • 当活动成为用户关注的焦点时,软键盘的状态 - 无论是隐藏还是可见.
  • 对活动主窗口进行调整 - 是否调整较小以便为软键盘腾出空间,或者当软键盘覆盖部分窗口时,其内容是否平移以使当前焦点可见.

介绍于:

  • API级别3.

链接到文档

注意: 此处设置的值("stateUnspecified"和"adjustUnspecified"除外)将覆盖主题中设置的值.

  • 如果您在活动中混用了多个文本字段,其中一些要使用键盘,而有些则不需要,则这种方法将失败。请参阅我的答案,以了解在这种情况下如何在每个文本字段的基础上实现这一目标。 (2认同)

Hou*_*fly 81

您必须在EditText上方创建一个视图,该视图采用"假"焦点:

就像是 :

<!-- Stop auto focussing the EditText -->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/transparent"
    android:focusable="true"
    android:focusableInTouchMode="true">
</LinearLayout>

<EditText
    android:id="@+id/searchAutoCompleteTextView_feed"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:inputType="text" />
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我使用LinearLayout来请求焦点.希望这可以帮助.

这完美地工作......感谢Zaggo0

  • 与将android:windowSoftInputMode ="stateHidden"添加到Activity的清单条目相比,添加无用视图是次优解决方案. (26认同)
  • 我尝试了所有解决方案,这是唯一正常工作的解决方案!谢谢清楚简单 (2认同)

Mat*_*556 37

     edittext.setShowSoftInputOnFocus(false);
Run Code Online (Sandbox Code Playgroud)

现在您可以使用您喜欢的任何自定义键盘.

  • 它可从API级别21获得 (20认同)

A.B*_*.B. 22

人们在这里提出了许多很好的解决方案,但我在EditText中使用了这个简单的技术(java中没有任何东西,并且需要AnroidManifest.xml).只需在EditText上直接将focusable和focusableInTouchMode设置为false即可.

 <EditText
        android:id="@+id/text_pin"
        android:layout_width="136dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:textAlignment="center"
        android:inputType="numberPassword"
        android:password="true"
        android:textSize="24dp"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
Run Code Online (Sandbox Code Playgroud)

我的意图是在App锁定活动中使用此编辑框,我要求用户输入PIN码,我想显示我的自定义密码键盘.在Android Studio 2.1上使用minSdk = 8和maxSdk = 23进行测试

在此输入图像描述


Son*_*ery 20

在您的活动类上添加以下代码.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

当用户单击EditText时,将弹出键盘


Luc*_*fer 9

您可以使用以下代码禁用OnScreen键盘.

InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)


kgs*_*mar 8

两个简单的方案:

第一个解决方案添加在清单xml文件中的代码行下面.在Manifest文件(AndroidManifest.xml)中,在activity构造中添加以下属性

机器人:windowSoftInputMode = "stateHidden"

例:

<activity android:name=".MainActivity" 
          android:windowSoftInputMode="stateHidden" />
Run Code Online (Sandbox Code Playgroud)

第二个解决方案是在活动中添加以下代码行

//Block auto opening keyboard  
  this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Run Code Online (Sandbox Code Playgroud)

我们可以使用上述任何一种解决方案.谢谢


And*_*eek 5

为InputMethodManager声明全局变量:

 private InputMethodManager im ;
Run Code Online (Sandbox Code Playgroud)

在onCreate()下定义它:

 im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 im.hideSoftInputFromWindow(youredittext.getWindowToken(), 0);
Run Code Online (Sandbox Code Playgroud)

将onClickListener设置为oncreate()内的编辑文本:

 youredittext.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        im.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
    }
});
Run Code Online (Sandbox Code Playgroud)

这会奏效.