如何防止软件键盘弹出?

发布于 2024-12-08 00:28:11 字数 447 浏览 0 评论 0原文

我的应用程序中有自己的键盘,因此我想始终隐藏软件键盘(在特定活动和对话框中)。 我尝试了两个选项:

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

此代码阻止键盘在开始时弹出,但是当我单击文本框时,键盘仍然弹出。

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

这段代码隐藏了键盘,但并不能阻止键盘弹出。

请帮忙!

I have my own keypad in my application so I want to hide the software keyboard all the time(in specific activities & dialogs).
I experimented with two options:

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

This code prevent the keyboard from popping up at the beginning, but when I click on the textbox the keyboard still pops.

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

This code hide the keyboard, but it doesn't prevent the keyboard from popping up.

PLEASE HELP!

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

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

发布评论

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

评论(1

Spring初心 2024-12-15 00:28:11

终于想通了!

我用于

public void supressKeyboard() {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
}

想要抑制键盘的活动(您可以将其放在所有其他活动继承的常规活动中)

但这不会阻止当您单击 EditText 文本框时弹出键盘。我所做的是消耗文本框的 onTouch 事件:

@Override
public boolean onTouchEvent(MotionEvent event) {
    return true;
}

Finally figured out!

I used

public void supressKeyboard() {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
}

for the activities where I want to suppress the keyboard(you can put it in a general activity where all other activities inherit from)

But this won't prevent the keyboard from popping up when you click on the EditText textbox. What I did is I consumed the onTouch event for the text box:

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