本文介绍了Android:如何永久&完全不显示EditText的默认软键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个活动中有三个EditText框,其中两个可以使用普通输入法(硬键,默认软键盘).但是对于EditText框之一,我只想从自定义键盘视图发送软输入.因此,实际上,我希望永远不要为该EditText显示默认的软键盘.我尝试为EditText添加onTouchListeners和onFocusChange侦听器,但部分成功,如下所示:

I have three EditText boxes in an activity, for two of which normal input methods (hard keys, default soft keyboard) are ok. But for one of the EditText boxes I want to send soft input only from a custom keyboard view. So in effect I want the default soft keyboard never to be shown for this EditText. I've tried adding onTouchListeners and onFocusChange listeners for the EditText with partial success like this:

public boolean onTouch(View v, MotionEvent event) {
    v.requestFocus();
    imm.toggleSoftInput(0, 0);
    return true;
}

public void onFocusChange(View v, boolean hasFocus) {
    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isActive(v)) {
        imm.toggleSoftInput(0,0);
    }
}

但由于

1)默认的软键盘总是短暂地闪烁可见,然后听众将其隐藏

1)the default soft keyboard always briefly flashes visible before the listener hides it

2)在某些情况下,例如使用硬键盘箭头键将焦点移至EditText有时会设置默认的软键盘可见

2)on some occasions, such as moving focus to the EditText with hard keyboard arrow keys sometimes sets the default soft keyboard visible

以此类推.

因此,我很想找到一种简单的方法来告诉Android永远不要显示此特定EditText的默认软键盘.我不想扩展EditText并开始覆盖东西,因为EditText功能对我来说是完美的-我只希望不显示默认的软键盘.

So I would love to find a simple way to tell Android never to show the default soft keyboard for this specific EditText. I would not like to extend EditText and start to override stuff, since the EditText functionality is perfect for me - I just want the default soft keyboard not to be shown.

我已经花了几天的时间试图解决这个问题.通过google找到的某些主题(包括此处的一些主题)在解决此问题时进行了中途尝试,但到目前为止,我还没有找到一个完全可用的解决方案.

I've spent days now trying to figure this out. Some topics (including some here) found via google have half-way attempts at this problem, but so far I haven't found a single totally functional solution.

我真的很生气.我决定我可以尝试不使用EditText,而可以使用任何其他可以完成工作的视图.事实证明,要摆脱该软键盘很难.当我使用硬键将焦点从EditText移到Button时,它甚至会显示出来!为什么到底应该在碰巧具有焦点的每个外观上显示软键盘?即使当我明确地说inputType ="none"时?如何关闭 * 软键盘?下面是Button的xml-让我们以它为例:

I'm really starting to get annoyed. I decided I could try not to use EditText but whatever other view that will get the job done. It turns out it is freakin hard to get rid of that soft keyboard. It even shows up when I use the hard keys to move focus from an EditText to a Button! Why on earth should the soft keyboard be shown on every freakin View that happens to have focus? Even when I explicitly say inputType="none"? How do I turn that * soft keyboard OFF? Below is the xml for the Button - let's use that as an example:

<Button
    android:id="@+id/OkButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="none"
    android:paddingRight="5mm"
    android:paddingLeft="5mm"
    android:layout_below="@id/Volume"
    android:layout_alignParentLeft="true"
    android:text="OK"/>

我如何实现了似乎可行的解决方案.首先,我掌握了InputMethodManager:

I have how achieved a solution that seems to work. First I get a hold of the InputMethodManager:

this.imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

当我希望EditText成为焦点并且我的自定义KeyboardView可见时,我设置的OnClickListener,OnTouchListener和OnFocusChange侦听器都调用以下方法,同时隐藏默认的软输入:

and the I set OnClickListener, OnTouchListener and OnFocusChange listener all call the following method when I want the EditText to be focused and my custom KeyboardView visible, while hiding the default soft input:

private boolean makeActive(View v) {
    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    EditText e = (EditText) v;
    int iType = e.getInputType();
    e.setInputType(InputType.TYPE_NULL);
    e.requestFocus();
    showKb();
    e.setInputType(iType);
    return true;
}

推荐答案

简单

editText.setShowSoftInputOnFocus(false);

有人建议以下内容可在旧版本的Android上运行,但这种行为是意外的.

Some people suggested that the following might work on older versions of Android, but the behaviour is unexpected.

edittextPlay.setTextIsSelectable(true);

这篇关于Android:如何永久&amp;完全不显示EditText的默认软键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:08