92

在我的应用程序中,我所有屏幕的第一个视图是 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)是焦点所在。

4

29 回答 29

182

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

安卓:windowSoftInputMode="stateHidden"


例子:

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

描述:

  • 当 Activity 成为用户关注的焦点时,软键盘的状态(无论是隐藏还是可见)。
  • 对 Activity 的主窗口进行的调整——是否将其调整为更小以为软键盘腾出空间,或者是否将其内容平移以使当前焦点在窗口的一部分被软键盘覆盖时可见。

介绍于:

  • API 级别 3。

链接到文档

注意: 此处设置的值(“stateUnspecified”和“adjustUnspecified”除外)会覆盖主题中设置的值。

于 2012-12-17T04:53:39.890 回答
84

您必须在 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" />

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

这工作得很好......感谢Zaggo0

于 2012-05-16T07:44:42.467 回答
45
     edittext.setShowSoftInputOnFocus(false);

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

于 2016-03-28T03:43:23.587 回答
23

人们在这里提出了许多很好的解决方案,但我在 EditText 中使用了这种简单的技术(java 和 AnroidManifest.xml 中不需要任何内容​​)。只需在 EditText 上直接将您的可聚焦和可聚焦InTouchMode 设置为 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 进行测试

在此处输入图像描述

于 2016-11-16T14:42:57.570 回答
20

在您的活动课程中添加以下代码。

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

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

于 2015-02-06T11:57:37.487 回答
10

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

InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(editText.getWindowToken(), 0);
于 2012-05-16T04:06:21.857 回答
8

两个简单的解决方案:

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

安卓:windowSoftInputMode="stateHidden"

例子:

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

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

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

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

于 2016-10-17T04:45:00.597 回答
5

为 InputMethodManager 声明全局变量:

 private InputMethodManager im ;

在 onCreate() 下定义它:

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

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

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

这将起作用。

于 2012-05-16T06:06:52.567 回答
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);
于 2012-05-18T07:02:38.437 回答
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);
}

默认情况下,您可以在计算器上输入任何内容,但您可以设置任何字符串。

尝试一下

于 2016-05-28T07:20:27.303 回答
3

好吧,我遇到了同样的问题,我只是在 XML 文件中处理了可聚焦的问题。

<EditText
            android:cursorVisible="false"
            android:id="@+id/edit"
            android:focusable="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

您可能也在寻找安全性。这也将对此有所帮助。

于 2017-07-03T10:53:05.347 回答
3

感谢@AB 提供好的解决方案

    android:focusableInTouchMode="false"

在这种情况下,如果您要在编辑文本中禁用键盘,只需在编辑文本标语中添加 android:focusableInTouchMode ="false"即可。

在 Android Studio 3.0.1 minsdk 16 , maxsdk26 中为我工作

于 2018-03-12T19:18:53.020 回答
2

试试这个:

EditText yourEditText= (EditText) findViewById(R.id.yourEditText); 
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT); 

要关闭,您可以使用:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
 imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0); 

在您的代码中尝试这样的操作:

ed = (EditText)findViewById(R.id.editText1);

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);  

ed.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
        imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);  
    }
});
于 2012-05-16T04:14:55.017 回答
2

您唯一需要做的就是添加android:focusableInTouchMode="false"到 xml 中的 EditText 等等。(如果有人仍然需要知道如何用简单的方法做到这一点)

于 2017-01-21T10:36:44.673 回答
1

在您的方法中使用以下代码onCreate()-

    editText = (EditText) findViewById(R.id.editText);
    editText.requestFocus();
    editText.postDelayed(new Runnable() {
        public void run() {
            InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.hideSoftInputFromWindow(
                    editText.getWindowToken(), 0);
        }
    }, 200);
于 2014-06-07T10:41:30.723 回答
1

对于 Xamarin 用户:

[Activity(MainLauncher = true, 
        ScreenOrientation = ScreenOrientation.Portrait, 
        WindowSoftInputMode = SoftInput.StateHidden)] //SoftInput.StateHidden - disables keyboard autopop
于 2017-05-11T07:01:36.957 回答
1
       <TextView android:layout_width="match_parent"
                 android:layout_height="wrap_content"

                 android:focusable="true"
                 android:focusableInTouchMode="true">

                <requestFocus/>
      </TextView>

      <EditText android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
于 2017-09-12T09:17:40.703 回答
1

如果您使用的是 Xamarin,则可以添加此

Activity[(WindowSoftInputMode = SoftInput.StateAlwaysHidden)]

此后您可以在 OnCreate() 方法中添加此行

youredittext.ShowSoftInputOnFocus = false;

如果目标设备不支持上述代码,可以在EditText点击事件中使用下面的代码

InputMethodManager Imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
Imm.HideSoftInputFromWindow(youredittext.WindowToken, HideSoftInputFlags.None);
于 2017-10-02T08:26:22.847 回答
1

我发现以下模式在我想显示一个对话框以获取输入的代码中对我很有效(例如,文本字段中显示的字符串是从对话框中的复选框列表中进行选择的结果,而不是通过键盘输入的文本)。

  1. 禁用编辑字段上的软输入焦点。我无法禁用整个活动,因为我确实希望在相同布局中使用键盘的编辑字段。
  2. 文本字段中的初始单击会产生焦点更改,重复单击会产生单击事件。所以我覆盖了两者(这里我不重构代码来说明两个处理程序做同样的事情):

    tx = (TextView) m_activity.findViewById(R.id.allergymeds);
    if (tx != null) {
        tx.setShowSoftInputOnFocus(false);
        tx.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    MedicationsListDialogFragment mld = new MedicationsListDialogFragment();
                    mld.setPatientId(m_sess.getActivePatientId());
                    mld.show(getFragmentManager(), "Allergy Medications Dialog");
                }
            }
        });
        tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MedicationsListDialogFragment mld = new MedicationsListDialogFragment();
                mld.setPatientId(m_sess.getActivePatientId());
                mld.show(getFragmentManager(), "Allergy Medications Dialog");
            }
        });
    }
    
于 2017-12-20T08:19:21.113 回答
0

在我正在构建的一个 Android 应用程序中,我有三个水平排列EditText的 s 。LinearLayout我必须防止在加载片段时出现软键盘。除了设置focusablefocusableInTouchMode为 true之外LinearLayout,我还必须设置descendantFocusabilityblocksDescendants。在onCreate,我打电话requestFocusLinearLayout。这可以防止在创建片段时出现键盘。

布局 -

    <LinearLayout
       android:id="@+id/text_selector_container"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:weightSum="3"
       android:orientation="horizontal"
       android:focusable="true"
       android:focusableInTouchMode="true"
       android:descendantFocusability="blocksDescendants"
       android:background="@color/black">

       <!-- EditText widgets -->

    </LinearLayout>

onCreate-mTextSelectorContainer.requestFocus();

于 2016-10-12T20:09:18.803 回答
0

如果有人仍在寻找最简单的解决方案,请将以下属性设置为true您的父布局

android:focusableInTouchMode="true"

例子:

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="true">

.......
......
</android.support.constraint.ConstraintLayout>
于 2017-04-08T15:07:12.533 回答
0

使用它来启用和禁用 EditText....

InputMethodManager imm;

imm = (InputMethodManager)
getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);

if (isETEnable == true) {

    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    ivWalllet.setImageResource(R.drawable.checkbox_yes);
    etWalletAmount.setEnabled(true);
    etWalletAmount.requestFocus();
    isETEnable = false;
    } 
else {

    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
    ivWalllet.setImageResource(R.drawable.checkbox_unchecked);
    etWalletAmount.setEnabled(false);
    isETEnable = true;
    }
于 2017-05-09T10:48:40.720 回答
0

试试这个答案,

editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);

注意:仅适用于 API 11+

于 2017-05-20T12:44:35.853 回答
0
private InputMethodManager imm;

...


editText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.onTouchEvent(event);
        hideDefaultKeyboard(v);
        return true;
    }
});

private void hideDefaultKeyboard(View et) {
  getMethodManager().hideSoftInputFromWindow(et.getWindowToken(), 0);
}

private InputMethodManager getMethodManager() {
        if (this.imm == null) {
            this.imm = (InputMethodManager)  getContext().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
        }
  return this.imm;
}
于 2017-05-21T05:56:04.820 回答
0

当您想在任何事件上隐藏屏幕上的键盘时,请尝试以下代码行。它对我有用。

context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );

final InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);

if (inputMethodManager.isAcceptingText())       inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);
于 2021-03-22T09:49:50.707 回答
-1

对于您活动中的任何文本视图(或使用 android:layout_width="0dp" android:layout_height="0dp" 创建假的空文本视图)并为该文本视图添加下一个: android:textIsSelectable="true"

于 2016-11-11T11:29:57.740 回答
-1

只需<request focus/>从 xml 文件中删除标签。

于 2018-07-05T08:42:12.310 回答
-1

只需要在布局 xmlandroid:focusable="false"中特别添加属性。EditText然后你可以编写点击列表器EditText而不弹出键盘。

于 2018-11-14T09:38:03.467 回答
-1

问题可以使用排序,无需将editText inputType设置为任何值,只需添加以下行editText.setTextIsSelectable(true);

于 2018-12-05T13:58:44.773 回答