gpt4 book ai didi

android - 使用 InputMethodManager.showSoftKeyboard() 时如何禁用自动完成/建议

转载 作者:行者123 更新时间:2023-11-30 04:58:28 24 4
gpt4 key购买 nike

试图修复我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com