如何在EditText上隐藏Android软键盘

时间:2012-01-25 02:38:52

标签: android android-softkeyboard

我有一个带有一些EditText字段和一些按钮的Activity,以方便通常用于填充这些字段的内容。但是,当我们用户触摸其中一个EditText字段时,会自动显示Android软键盘。我希望它默认保持隐藏状态,除非用户长按菜单按钮。我已经找到了解决方案,并找到了几个答案,但到目前为止我无法让它们起作用。

我尝试了以下内容:

1 - 在onCreate方法中,

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

2 - 同样在onCreate方法中,

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

3 - 并且在清单文件中,

<activity android:name=".activityName" android:windowSoftInputMode="stateAlwaysHidden"/>

这些方法都不起作用。只要用户单击EditText字段,就会出现软键盘。如果用户通过长按菜单键明确显示软键盘,我只希望显示软键盘。

为什么这不起作用?

17 个答案:

答案 0 :(得分:84)

这会对你有所帮助

editText.setInputType(InputType.TYPE_NULL);

编辑:

要显示软键盘,您必须在long key press event of menu button

中编写以下代码
editText.setInputType(InputType.TYPE_CLASS_TEXT);
            editText.requestFocus();
            InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            mgr.showSoftInput(editText, InputMethodManager.SHOW_FORCED);

答案 1 :(得分:31)

您需要在Activity中为AndroidManifest.xml添加以下属性。

<activity
    ...
    android:windowSoftInputMode="stateHidden|adjustResize"
    ...
/>

答案 2 :(得分:8)

我有时会使用一些技巧来做到这一点。我在布局的顶部放置了一个隐形焦点支架。这将是例如像这样

 <EditText android:id="@id/editInvisibleFocusHolder"
          style="@style/InvisibleFocusHolder"/>

这种风格

<style name="InvisibleFocusHolder">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">0dp</item>
    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
    <item name="android:inputType">none</item>
</style>

然后在onResume中我会调用

    editInvisibleFocusHolder.setInputType(InputType.TYPE_NULL);
    editInvisibleFocusHolder.requestFocus();

对我来说这很好用,从1.6到4.x

答案 3 :(得分:7)

经过长时间查看TextView类,我发现了一种防止键盘出现的方法。诀窍是它出现后立即隐藏它,所以我搜索了键盘出现后调用的方法并将其隐藏起来。

实施EditText类

public class NoImeEditText extends EditText {

    public NoImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * This method is called before keyboard appears when text is selected.
     * So just hide the keyboard
     * @return
     */
    @Override
    public boolean onCheckIsTextEditor() {
        hideKeyboard();

        return super.onCheckIsTextEditor();
    }

    /**
     * This methdod is called when text selection is changed, so hide keyboard to prevent it to appear
     * @param selStart
     * @param selEnd
     */
    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);

        hideKeyboard();
    }

    private void hideKeyboard(){
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindowToken(), 0);
    }
}

和风格

<com.my.app.CustomViews.NoImeEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:editable="false"
    android:background="@null"
    android:textSize="@dimen/cell_text" />

答案 4 :(得分:6)

即使我将EditorInfo.TYPE_NULL设置为视图,软键盘也会继续上升。 除了我从nik431的答案得到的想法之外,没有一个答案对我有用:

editText.setCursorVisible(false);
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);

答案 5 :(得分:2)

似乎有多种方法可以阻止系统键盘以编程方式和xml方式出现。但是,这是支持API 11之前设备的方式。

// prevent system keyboard from appearing
if (android.os.Build.VERSION.SDK_INT >= 11) {
    editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
    editText.setTextIsSelectable(true);
} else {
    editText.setRawInputType(InputType.TYPE_NULL);
    editText.setFocusable(true);
}

答案 6 :(得分:2)

以下行正是所寻找的内容。此方法已包含在API 21中,因此适用于API 21及更高版本。

edittext.setShowSoftInputOnFocus(false);

答案 7 :(得分:1)

让我们尝试在xml中为EditText

设置以下属性
android:focusableInTouchMode="true" android:cursorVisible="false".

如果您想在启动活动时隐藏软键,请仔细阅读link

答案 8 :(得分:1)

我的测试结果:

setInputType

editText.setInputType(InputType.TYPE_NULL);

软键盘消失,但光标也会消失。

setShowSoftInputOnFocus

editText.setShowSoftInputOnFocus(false)

它按预期工作。

答案 9 :(得分:0)

weekText = (EditText) layout.findViewById(R.id.weekEditText);
weekText.setInputType(InputType.TYPE_NULL);

答案 10 :(得分:0)

基于相同简单指令的三种方式:

A)。结果像locate(1)一样简单:

android:focusableInTouchMode="true"

布局中任何先前元素的配置,例如:

如果您的整个布局由以下内容组成:

<ImageView>

<EditTextView>

<EditTextView>

<EditTextView>

然后你可以在ImageView参数中编写(1),这将抓住Android对ImageView而不是EditText的注意。

B)。如果您有另一个先前元素而不是ImageView,您可能需要将(2)添加到(1)中:

android:focusable="true"

C)。您还可以在视图元素的顶部创建一个空元素:

<LinearLayout
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:layout_width="0px"
  android:layout_height="0px" />

这个替代方案直到这一点成为我所见过的最简单的结果。希望它有所帮助...

答案 11 :(得分:0)

隐藏键盘

editText.setInputType(InputType.TYPE_NULL);

显示键盘

etData.setInputType(InputType.TYPE_CLASS_TEXT);
etData.setFocusableInTouchMode(true);
父布局中的

android:focusable="false"

答案 12 :(得分:0)

简单使用 活动中的EditText.setFocusable(false);

或 在xml中使用

android:focusable="false"

答案 13 :(得分:0)

你好,使用此代码对我来说100%有效

EditText ee=new EditText(this);
ee.setShowSoftInputOnFocus(false);

*但是您只想在edittext上隐藏键盘,单击下面的代码即可,但是键盘图标和后退按钮的向上符号将创建 *给出的代码是这样的

        EditText ee=new EditText(this);//create edittext
        final   View.OnTouchListener disable =new View.OnTouchListener() {
        public boolean onTouch (View v, MotionEvent event) {
        v.onTouchEvent(event);
        InputMethodManager img = 
        (InputMethodManager)v.getContext().getSystemService(INPUT_METHOD_SERVICE);
        if (img != null) {
        img.hideSoftInputFromWindow(v.getWindowToken(),0);
        }
        return true;
        }
        };
        //call touch listener
       ee.setOnTouchListener(disable);

在更改文本的侦听器时也使用它

        EditText ee= new EditText(this);
        Text=new TextWatcher(){

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int 
        after) {
        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }
       @Override
        public void afterTextChanged(Editable s) {
        InputMethodManager img = (InputMethodManager) 
        getSystemService(INPUT_METHOD_SERVICE);
        img.hideSoftInputFromWindow(ee.getWindowToken(), 0);

        }
        };
       ee.addTextChangedListener(Text);

在onclick侦听器中也使用它

 EditText ee=new EditText(this);
 ee.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {

 InputMethodManager img= (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
 img.hideSoftInputFromWindow(ee.getWindowToken(),0);

 }});

所有代码都可以使用,但对我而言,最好的代码是

答案 14 :(得分:0)

只需使用以下方法

private fun hideKeyboard(activity: Activity, editText: EditText) {
    editText.clearFocus()
    (activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).hideSoftInputFromWindow(editText.windowToken, 0)
}

答案 15 :(得分:0)

   public class NonKeyboardEditText extends AppCompatEditText {

    public NonKeyboardEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}

并添加

NonKeyboardEditText.setTextIsSelectable(true);

答案 16 :(得分:0)

我也遇到了同样的问题,我通过这个方法解决了这个问题,

   editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // do something..
            }
            
                closeKeyborad();
                return true;
            }
            return false;
        }
    });

在返回 true 之前调用该函数。

private void closeKeyborad() {
    View view = this.getCurrentFocus();
    if (view != null){
        InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken() , 0);
    }
}