- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
试图修复我的 LibGDX 应用程序中的一个错误,一些 Android 用户报告说空格键会在他们的输入中插入“自动完成”字样。在某些情况下,软键盘甚至不显示自动完成建议。
据我所知,LibGDX 不使用“原生”Android UI 元素,因此 TextField
对象在点击时运行此代码:
public void setOnscreenKeyboardVisible (final boolean visible) {
handle.post(new Runnable() {
public void run () {
InputMethodManager manager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (visible) {
View view = ((AndroidGraphics)app.getGraphics()).getView();
view.setFocusable(true);
view.setFocusableInTouchMode(true);
manager.showSoftInput(view, 0);
} else {
manager.hideSoftInputFromWindow(((AndroidGraphics)app.getGraphics()).getView().getWindowToken(), 0);
}
}
});
}
所以这里有一行显示键盘:
manager.showSoftInput(view, 0);
我如何设置一些标志或其他东西来告诉键盘禁用自动完成?
请注意,这只发生在某些用户身上,找不到模式。此外,到目前为止,仅限 Android 8 和 9。
最佳答案
我搜索了一下,发现这根本不是直截了当的。
您显示的 AndroidInput
中的方法使用 View app.getGraphics().getView()
来显示键盘。如 another answer 中所述,您需要对该 View 进行子类化并覆盖以下方法才能设置输入标志:
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
// Set flags to disable suggestions. Visible password flag is only needed for some devices.
outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
| InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
return connection;
}
除了 AndroidGraphics
使用的 View 不是特别容易子类化。相反,您可以子类化 AndroidInput
类并提供不同的输入 View 。这是一个示例启动器类:
class AndroidLauncher extends AndroidApplication {
private View inputView;
private Handler handle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Config and init your game class.
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new MyGame(), config);
// Custom view used for showing the keyboard.
// Most of the code in onCreateInputConnection comes from the GLSurfaceView20 class.
inputView = new View(this) {
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
if (outAttrs != null) {
outAttrs.imeOptions |= EditorInfo.IME_FLAG_NO_EXTRACT_UI;
// This line is the only different from original source, it disables suggestions.
outAttrs.inputType |= InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
| InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
}
return new BaseInputConnection(this, false) {
@Override
public boolean deleteSurroundingText (int beforeLength, int afterLength) {
int sdkVersion = android.os.Build.VERSION.SDK_INT;
if (sdkVersion >= 16) {
if (beforeLength == 1 && afterLength == 0) {
sendDownUpKeyEventForBackwardCompatibility(KeyEvent.KEYCODE_DEL);
return true;
}
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
@TargetApi(16)
private void sendDownUpKeyEventForBackwardCompatibility (final int code) {
final long eventTime = SystemClock.uptimeMillis();
super.sendKeyEvent(new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, code, 0, 0,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
super.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime, KeyEvent.ACTION_UP, code, 0, 0,
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
}
};
}
};
handle = new Handler(); // handle is private in AndroidInput so we create our own.
// Android has two different AndroidInput classes depending on SDK versions, create the right one. You can remove one of them if you don't support API < 12.
if (Build.VERSION.PREVIEW_SDK_INT >= 12) {
input = new AndroidInputThreePlus(this, this, graphics.getView(), config) {
@Override
public void setOnscreenKeyboardVisible(boolean visible) {
AndroidLauncher.this.setOnscreenKeyboardVisible(visible);
}
};
} else {
input = new AndroidInput(this, this, graphics.getView(), config) {
@Override
public void setOnscreenKeyboardVisible(boolean visible) {
AndroidLauncher.this.setOnscreenKeyboardVisible(visible);
}
};
}
}
private void setOnscreenKeyboardVisible(final boolean visible) {
// Code comes from AndroidInput.setOnscreenKeyboardVisible except view is changed.
handle.post(new Runnable() {
public void run () {
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (visible) {
inputView.setFocusable(true);
inputView.setFocusableInTouchMode(true);
manager.showSoftInput(inputView, 0);
} else {
manager.hideSoftInputFromWindow(inputView.getWindowToken(), 0);
}
}
});
}
}
如您所见,这并不容易做到。我也没有测试过它,所以我可能会遗漏更多东西。
那时我宁愿建议您在 github 上提交问题并提出拉取请求。 android 图形 View 实现是 GLSurfaceView20
(或 GLSurfaceView20API18
),它已经覆盖了 onCreateInputConnection
。您可以在 Input
接口(interface)中添加一个选项来禁用建议,并在每个后端实现它。
如果您对此有任何疑问,请随时提出。
关于android - 使用 InputMethodManager.showSoftKeyboard() 时如何禁用自动完成/建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58718736/
在Android中,inputmethodmanagerapi来管理输入法,现在有个问题,如何通过inputmethodmanager类型来控制输入法,比如数字键盘或者字母键盘等? InputMeth
是否可以从键盘获取当前输入类型(文本或数字)?或者是否有输入类型更改时触发的事件?我需要根据用户退出该屏幕时的输入类型在屏幕上设置输入类型。谢谢 最佳答案 editText.getInputType(
我想做的是用下面的代码显示键盘 InputMethodManager input = (InputMethodManager) getSystemService(Context.INPUT_METHO
我正在尝试在我的 Activity 中手动显示/隐藏软键盘,以便在焦点上编辑 TextView ,但在某些设备上,它显示 sys/err 并在我的应用程序中导致崩溃或 ANR,并显示以下堆栈跟踪 Fa
我是 Android 编程的新手,所以只是制作一个简单的应用程序。 在应用程序中,我有一个 EditText 组件,它会在键盘弹出时向上滑动,我不希望它向上滑动,所以我搜索了它并解决了使用问题, 在
类 InputMethodManager 中的方法 showSoftInput 的目的是什么?我是 android 的新手,对我来说很好 documentation不是很清楚: Explicitly
所以我有一个使用 windowSoftInputMode="adjustPan" 的 Activity,并且我有一个 OnPreDrawListener 用于 EditText,它调用: editTe
给出以下代码: InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD
我正在使用 InputMethodManager 在需要时以编程方式显示软键盘 InputMethodManager imm = (InputMethodManager)getSystemServic
我正在尝试使用以下代码在用户单击按钮时隐藏软键盘: InputMethodManager imm = (InputMethodManager) getSystemService(Activity.IN
我试图在屏幕上弹出一个软键盘,首先以编程方式加载(不更改 list 中的 windowSoftInputMode)。 有趣的是在屏幕上首次加载时,它根本不起作用。这是代码块。 mEDT.request
InputMethodManager 是应用程序可用于与系统键盘交互的服务。 EditText 等编辑器也使用它来间接通知键盘更改(例如,updateSelection)。 我可以像这样获得对 Inp
我正试图找出我的应用程序中的泄漏,但我不确定这是从哪里来的。 LeakCanary 告诉我可以忽略它。是吗? 01-06 12:04:56.580 6935-9159/com.mypackage D/
试图修复我的 LibGDX 应用程序中的一个错误,一些 Android 用户报告说空格键会在他们的输入中插入“自动完成”字样。在某些情况下,软键盘甚至不显示自动完成建议。 据我所知,LibGDX 不使
我有一些代码: inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0, new Result
嗨,我正在尝试输入这段代码来隐藏 Android 上的软键盘,但它返回一个空指针。 代码: public void testSetTestEnvironment (){ solo.clickI
最近在开发一个应用程序时,我遇到了一个问题。我在谷歌上搜索了很多但找不到任何解决方案。最后我遇到了这个 Android issue tracker 为了解释我的问题,我制作了一个示例应用程序。 我的示
我关注了来自 here 的“避免内存泄漏”文章. 但是,建议的解决方案并不能解决泄漏问题。我在 Windows XP (SDK 2.3.1) 上使用 android 模拟器对此进行了测试。我转储了堆并
在运行我的 Android 应用程序时,我在应用程序生成 ANR 弹出警告之前在日志中看到以下警告级别的消息: “InputMethodManager:当前服务 View 不是焦点 View ” 我在
我想知道常量 SHOW_FORCED 和 SHOW_IMPLICIT 的行为差异。我两种都试过了,第一眼看不出有什么不同。 最佳答案 SHOW_FORCED和 SHOW_IMPLICIT与隐藏方法协同
我是一名优秀的程序员,十分优秀!