Android:禁用所有 EditText 的软键盘

发布于 2024-11-03 08:00:39 字数 330 浏览 0 评论 0原文

我正在 Android 上使用一些 EditText 开发一个对话框。 我已将此行放在 onCreate() 中,以便禁用软键盘:

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

问题是它仅在对话框出现且不执行任何操作时才起作用。 当我移动到下一个 EditText 时,键盘会出现并且不会向下移动。

有人知道如何解决这个问题吗?

I am working on a dialog at Android with a few EditTexts.
I've put this line at the onCreate() in order to disable the soft keyboard:

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

The problem is that it works only when the dialog appear and doing nothing.
When I move to the next EditText, the keyboard appears and not going down.

Does anybody have an idea how to solve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

风追烟花雨 2024-11-10 08:00:40

创建您自己的类来扩展 EditText 并覆盖 onCheckIsTextEditor()

public class NoImeEditText extends EditText {
    public NoImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}

create your own class that extends EditText and override the onCheckIsTextEditor():

public class NoImeEditText extends EditText {
    public NoImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}
猫弦 2024-11-10 08:00:40

试试这个..

edittext.setInputType(InputType.TYPE_NULL);      
if (android.os.Build.VERSION.SDK_INT >= 11)   
{  
    edittext.setRawInputType(InputType.TYPE_CLASS_TEXT);  
    edittext.setTextIsSelectable(true);  
}

Try this out..

edittext.setInputType(InputType.TYPE_NULL);      
if (android.os.Build.VERSION.SDK_INT >= 11)   
{  
    edittext.setRawInputType(InputType.TYPE_CLASS_TEXT);  
    edittext.setTextIsSelectable(true);  
}
我一直都在从未离去 2024-11-10 08:00:40

我一整天都在寻找解决方案,并且遇到了这种方法。我把它放在这里是因为它似乎完美地回答了这个问题。

EditText et = ... // your EditText
et.setKeyListener(null) //makes the EditText non-editable so, it acts like a TextView.

不需要子类化。这与使 EditText 不可聚焦之间的主要区别在于,EditText 仍然有自己的光标 - 您可以选择文本等。它所做的只是抑制 IME 弹出自己的软键盘。

I have been looking for solutions to this all day, and I came across this approach. I'm putting it here because it seems to answer this question perfectly.

EditText et = ... // your EditText
et.setKeyListener(null) //makes the EditText non-editable so, it acts like a TextView.

No need to subclass. The main difference between this and making your EditText non-focusable, is that the EditText still has its own cursor - you can select text, etc. All it does is suppress the IME from popping up its own soft keyboard.

你是年少的欢喜 2024-11-10 08:00:40

调用TextView.setShowSoftInputOnFocus(false)。此方法自 API 级别 21 起就有记录,但自 API 级别 16 起就已存在(只是对 JavaDoc 隐藏了)。我在 AlertDialog 中将其与 API 级别 16 结合使用 dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

在API级别14中,有一个隐藏方法setSoftInputShownOnFocus(),它似乎具有相同的目的,但我没有测试过。

相对于InputType.TYPE_NULL 的优点是,可以使用所有正常输入类型(例如密码输入),并且触摸事件将光标定位在文本内的正确位置。

Call TextView.setShowSoftInputOnFocus(false). This method is documented since API level 21, but it's already there since API level 16 (it's just hidden from JavaDoc). I use it with API level 16 in an AlertDialog in combination with dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN).

In API level 14, there is a hidden method setSoftInputShownOnFocus() which seems to have the same purpose, but I have not tested that.

The advantage over InputType.TYPE_NULL is, that all the normal input types can be used (e.g. for password input) and the touch event positions the cursor at the correct spot within the text.

难理解 2024-11-10 08:00:40

这篇文章发布已经有一段时间了,但这里有一个对我有用的简单方法:在 xml 的 EditText 属性中,执行:android:focusable="false"。现在即使用户点击键盘也不会弹出。如果您提供自己的键盘,这非常有用。

Its been some time since this post, but here is a simple method which worked for me: in the xml, in the EditText properties, do: android:focusable="false". Now the keyboard will not popup even if the user clicks on it. This is useful if you are providing your own keypad.

薔薇婲 2024-11-10 08:00:40

将其放入 Oncreate 方法中

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

Put this in Oncreate Method

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
任谁 2024-11-10 08:00:40

知道为时已晚,但是您是否在布局中尝试过对 EditText 进行以下设置?

android:inputType="none"

更新

使用,

editText.setInputType(InputType.TYPE_NULL)
editText.setFocusable(false)

Know its too late, but have you tried the following setting to EditText in your layout ?

android:inputType="none"

UPDATE

Use,

editText.setInputType(InputType.TYPE_NULL)
editText.setFocusable(false)
神经大条 2024-11-10 08:00:40
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edSearch.getWindowToken(), 0);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edSearch.getWindowToken(), 0);
满栀 2024-11-10 08:00:40

为了禁用 ANDROID SOFT INPUT KEYBOARD xml 文件对我来说没有帮助
在 java 文件中的 EditText 对象上调用 setInputType 方法效果很好。
这是代码。

EditTextInputObj = (EditText) findViewById(R.id.EditTextInput);
EditTextInputObj.setInputType(InputType.TYPE_NULL); 

in order to disable ANDROID SOFT INPUT KEYBOARD xml file doesn't help in my case
calling the setInputType method on EditText object in java file works great.
here is the code.

EditTextInputObj = (EditText) findViewById(R.id.EditTextInput);
EditTextInputObj.setInputType(InputType.TYPE_NULL); 
無處可尋 2024-11-10 08:00:40

如果将 textView 放入视图组中,则可以使用以下命令使视图在其任何后代之前获得焦点:

view.setDescendantFocusability(view.FOCUS_BLOCK_DESCENDANTS);

If you put the textViews in the view group you can make make the view get the focus before any of its descendants by using this:

view.setDescendantFocusability(view.FOCUS_BLOCK_DESCENDANTS);
七色彩虹 2024-11-10 08:00:40

通过设置EditText focusable->false,点击时键盘不会打开

<EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:focusable="false" />

by set EditText focusable->false, keyboard will not opened when clicked

<EditText
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:focusable="false" />
对你的占有欲 2024-11-10 08:00:40

您可以这样做:

textView.setOnClickListener(null);

它将禁用 textView 的键盘,并且单击将不再起作用。

You can do that:

textView.setOnClickListener(null);

It will disable the keyboard to the textView and the click will not work anymore.

鹿港巷口少年归 2024-11-10 08:00:39

如果您查看 onCheckIsTextEditor() 方法实现(在 TextView 中),它看起来像这样:

@Override
public boolean onCheckIsTextEditor() {
    return mInputType != EditorInfo.TYPE_NULL;
}

这意味着您不必子类化,您可以:

((EditText) findViewById(R.id.editText1)).setInputType(InputType.TYPE_NULL); 

我尝试设置 android:inputType="none" 在布局 xml 中,但它对我不起作用,所以我以编程方式完成了它。

If you take look on onCheckIsTextEditor() method implementation (in TextView), it looks like this:

@Override
public boolean onCheckIsTextEditor() {
    return mInputType != EditorInfo.TYPE_NULL;
}

This means you don't have to subclass, you can just:

((EditText) findViewById(R.id.editText1)).setInputType(InputType.TYPE_NULL); 

I tried setting android:inputType="none" in layout xml but it didn't work for me, so I did it programmatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文