如何在EditText上编辑时禁用键盘弹出?

93

我的应用程序中,所有屏幕的第一个视图都是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);
            }
        }
    });

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>

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

29个回答

183

最佳解决方案在项目清单文件 (AndroidManifest.xml) 中,将以下属性添加到activity构造函数中

android:windowSoftInputMode="stateHidden"


示例:

    <activity android:name=".MainActivity" 
              android:windowSoftInputMode="stateHidden" />

描述:

  • 软键盘的状态,在该活动成为用户关注的焦点时,它是隐藏还是可见。
  • 为适应软键盘而调整活动主窗口的大小,或者当软键盘覆盖窗口的一部分时,其内容是否滚动以使当前焦点可见。

引入版本:

  • API Level 3。

文档链接

注意:这里设置的值(除了"stateUnspecified"和"adjustUnspecified"之外)会覆盖在主题中设置的值。


3
如果活动中有多种文本字段需要处理,其中一些需要弹出键盘输入,而其他一些则不需要,那么这种方法就会失败。请参阅我的答案,了解在这种情况下如何根据每个文本字段单独设置。 - slogan621
@slogan621请花点时间理解这里的问题。你走了一条不同的路线! - Skynet

85
你需要创建一个视图,在EditText上方,它具有'fake'焦点:

类似于:

<!-- 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" />

在这种情况下,我使用了LinearLayout来请求焦点。希望这可以帮到你。

这完美地解决了问题...感谢Zaggo0。


26
与在Activity的清单项中添加android:windowSoftInputMode="stateHidden"相比,添加一个无用的视图是次优的解决方案。 - Chris Horner
@ChrisHorner 在这两种情况下,您都需要指定活动或布局... 这是相同的烦人问题... 对于此解决方案,两者都可以很好地工作。 - Nicolas Jafelle
像@ChrisHorner所说,添加一个无用的视图并不是一个好主意,最好像他建议的那样在清单文件中添加一个属性或将这行代码添加到您的活动中 `InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0);` - aB9
我必须同意Housefly的答案。ChrisHorner的答案适用于一项活动,但如果您有一个带有输入的警报对话框,并且不想显示键盘怎么办?这是一个很好的解决方案,只需在其上方添加一个具有0维度的“空格”。 - Seth
1
这个视图层次结构的实现太糟糕了!我认为如果可能的话应该避免这种情况。 - sud007
显示剩余3条评论

45
     edittext.setShowSoftInputOnFocus(false);

现在您可以使用任何自定义键盘。


21
可从 API 级别 21 起使用。 - Jithu P.S
@JithuP.S,为什么它能在API级别为19的Karbon钛上运行? - LightSith
这看起来正是我需要的方法,但不幸的是它并不能防止键盘显示。有什么原因可以解释吗? - lorenzo

23

这里有很多优秀的解决方案,但我使用了一种简单的技巧来处理我的EditText(不需要在Java和AnroidManifest.xml中进行任何设置)。只需直接将您的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"/>

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

输入图像描述


这让我的生活变得轻松了。https://dev59.com/WGkw5IYBdhLWcg3w4emS - Mohammed mansoor

20
在您的活动类中添加以下代码。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
用户点击EditText时,键盘将弹出。

10

您可以使用以下代码来禁用屏幕键盘。

InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(editText.getWindowToken(), 0);

1
我可以通过使用EditText.onClick(im.unhideSoftInputFromWindow(editText.getWindowToken(), 0);)或类似的方式重新启用它吗? - Housefly

8

两个简单的解决方案:

第一个解决方案是在清单xml文件中添加以下代码行。在Manifest文件(AndroidManifest.xml)中,将以下属性添加到activity构造函数中:

android:windowSoftInputMode="stateHidden"

示例:

<activity android:name=".MainActivity" 
          android:windowSoftInputMode="stateHidden" />

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

//Block auto opening keyboard  
  this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

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

5

声明InputMethodManager的全局变量:

 private InputMethodManager im ;

在onCreate()中定义它:
 im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
 im.hideSoftInputFromWindow(youredittext.getWindowToken(), 0);

在oncreate()中将onClickListner设置为该编辑文本:
 youredittext.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        im.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
    }
});

这将起作用。

当我点击编辑文本时,屏幕键盘没有显示出来。所有的代码都在问题中。 - Housefly
你是在使用模拟器还是设备进行测试?如果使用模拟器测试,则无法获取软按键。请使用设备进行测试,它将正常运行。 - AndroGeek
是的,我正在使用设备进行测试。 - Housefly
嗯,这很奇怪。如果您能发布您的代码以及编写方式,那么可能会有所帮助。否则,上面的代码在我的设备上完全正常运行。 - AndroGeek
哦...但没问题,它已经解决了...不过是以完全不同的方式...无论如何还是谢谢你...检查我的答案...那也可能会帮到你 :) - Housefly

4

尝试一下......我使用以下代码解决了这个问题:

EditText inputArea;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
inputArea = (EditText) findViewById(R.id.inputArea);

//This line is you answer.Its unable your click ability in this Edit Text
//just write
inputArea.setInputType(0);
}

默认计算器只能接受数字输入,但是你可以设置任何字符串来代替数字。

试一下吧。


4
请在 onCreate() 下方编写以下代码:
InputMethodManager inputManager = (InputMethodManager)
                                   getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                   InputMethodManager.HIDE_NOT_ALWAYS);         
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接