Android 如何在编辑文本上禁用键盘弹出窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10611833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:22:00  来源:igfitidea点击:

How to disable keypad popup when on edittext?

androidandroid-edittext

提问by Housefly

In my app the first view of all my screens is an EditText, so every time i go to a screen the onscreen keypad pops up. how can i disable this popingup and enable it when manually clicked on the 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);
            }
        }
    });

xml code:

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>

the third view (EditText) is where the focus is.

第三个视图 (EditText) 是焦点所在。

回答by Skynet

The best solution lies in the Project Manifest file (AndroidManifest.xml), add the following attribute in the activityconstruct

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

android:windowSoftInputMode="stateHidden"

android:windowSoftInputMode="stateHidden"



Example:

例子:

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

Description:

描述:

  • The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
  • The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.
  • 当 Activity 成为用户关注的焦点时,软键盘的状态——无论是隐藏还是可见。
  • 对 Activity 的主窗口所做的调整 - 是否将其调整为更小以便为软键盘腾出空间,或者当窗口的一部分被软键盘覆盖时其内容是否平移以使当前焦点可见。

Introduced in:

介绍于:

  • API Level 3.
  • API 级别 3。

Link to the Docs

链接到文档

Note:Values set here (other than "stateUnspecified" and "adjustUnspecified") override values set in the theme.

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

回答by Housefly

You have to create a view, above the EditText, that takes a 'fake' focus:

您必须在 EditText 上方创建一个具有“假”焦点的视图:

Something like :

就像是 :

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

In this case, I used a LinearLayout to request the focus. Hope this helps.

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

This worked perfectly...thanks to Zaggo0

这非常有效......感谢Zaggo0

回答by Matthew556

     edittext.setShowSoftInputOnFocus(false);

Now you can use any custom keyboard you like.

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

回答by A.B.

People have suggested many great solutions here, but I used this simple technique with my EditText (nothing in java and AnroidManifest.xml is required). Just set your focusable and focusableInTouchMode to false directly on EditText.

人们在这里提出了许多很好的解决方案,但我在我的 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"/>

My intent here is to use this edit box in App lock activity where I am asking user to input the PIN and I want to show my custom PIN pad. Tested with minSdk=8 and maxSdk=23 on Android Studio 2.1

我的目的是在 App lock 活动中使用这个编辑框,我要求用户输入 PIN 码,并且我想显示我的自定义 PIN 键盘。在 Android Studio 2.1 上使用 minSdk=8 和 maxSdk=23 进行测试

enter image description here

在此处输入图片说明

回答by Sonia John Kavery

Add below code on your activity class.

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

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

Keyboard will pop-upped when user clicks on the EditText

当用户点击 EditText 时会弹出键盘

回答by Lucifer

You can use following code for disabling OnScreen Keyboard.

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

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

回答by kgsharathkumar

Two Simple Solutions:

两个简单的解决方案:

First Solutionis added below line of code in manifest xml file. In Manifest file (AndroidManifest.xml), add the following attribute in the activity construct

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

android:windowSoftInputMode="stateHidden"

android:windowSoftInputMode="stateHidden"

Example:

例子:

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

Second Solutionis adding below line of code in activity

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

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

We can use any one solution of above. Thanks

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

回答by AndroGeek

Declare the global variable for InputMethodManager:

为 InputMethodManager 声明全局变量:

 private InputMethodManager im ;

Under onCreate() define it:

在 onCreate() 下定义它:

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

Set the onClickListener to that edit text inside oncreate():

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

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

This will work.

这将起作用。

回答by Dharmaraj Patil

Use the following code, write it under onCreate()

使用下面的代码,写在下面 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);

回答by S.M.Fazle Rabbi

try it.......i solve this problem using code:-

试试吧.......我使用代码解决了这个问题:-

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

nothing u can input by default calculator on anything but u can set any string.

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

try it

尝试一下