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

Android 如何在使用InputMethodManager.showSoftKeyboard()时禁用自动完成/建议,android,libgdx,Android,Libgdx,试图修复我的LibGDX应用程序中的一个bug,其中一些安卓用户报告空格键在他们的输入中插入了“自动完成”单词。在某些情况下,软键盘甚至不显示自动完成建议 据我所知,LibGDX不使用“本机”Android UI元素,因此TextField对象在单击时运行此代码: public void setOnscreenKeyboardVisible (final boolean visible) { handle.post(new Runnable() { pub

试图修复我的LibGDX应用程序中的一个bug,其中一些安卓用户报告空格键在他们的输入中插入了“自动完成”单词。在某些情况下,软键盘甚至不显示自动完成建议

据我所知,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);
如何设置一些标志或指示键盘禁用自动完成功能


注意,这只发生在一些用户身上,找不到模式。另外,到目前为止,只有安卓8和9。我搜索了一下,结果发现这一点都不简单

您显示的
AndroidInput
中的方法使用视图
app.getGraphics().getView()
显示键盘。如中所述,您需要对该视图进行子类化,并重写以下方法才能设置输入标志:

@覆盖
公共输入连接onCreateInputConnection(EditorInfo outAttrs){
InputConnection=super.onCreateInputConnection(outAttrs);
//设置标志以禁用建议。只有某些设备才需要可见的密码标志。
outAttrs.inputType |=inputType.TYPE_文本_标志_无建议
|InputType.TYPE\u TEXT\u VARIATION\u VISIBLE\u PASSWORD;
回路连接;
}
除了
AndroidGraphics
使用的视图之外,子类化并不特别容易。相反,您可以对
AndroidInput
类进行子类化,并为输入提供不同的视图。下面是一个示例启动器类:

类AndroidLauncher扩展了AndroidApplication{ 私有视图输入视图; 私人经办人处理; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); //配置并初始化游戏类。 AndroidApplicationConfiguration配置=新的AndroidApplicationConfiguration(); 初始化(新建MyGame(),配置); //用于显示键盘的自定义视图。 //onCreateInputConnection中的大部分代码来自GLSurfaceView20类。 inputView=新视图(此){ @凌驾 公共输入连接onCreateInputConnection(EditorInfo outAttrs){ 如果(outAttrs!=null){ outAttrs.imeOptions |=EditorInfo.IME_标志(不)提取(UI); //这一行与原始源代码唯一不同,它禁用建议。 outAttrs.inputType |=inputType.TYPE_文本_标志_无建议 |InputType.TYPE\u TEXT\u VARIATION\u VISIBLE\u PASSWORD; } 返回新的BaseInputConnection(this,false){ @凌驾 公共布尔deleteSurroundingText(int beforeLength,int afterLength){ int-sdkVersion=android.os.Build.VERSION.SDK\u int; 如果(sdkVersion>=16){ if(beforeLength==1&&afterLength==0){ sendDownUpKeyEventForBackwardCompatibility(KeyEvent.keycodel); 返回true; } } 返回super.deleteSurroundingText(beforeLength,afterLength); } @塔吉塔皮(16) 专用void SendDownUpKeyEvents for BackwardCompatibility(最终整数代码){ 最终长事件时间=SystemClock.uptimeMillis(); super.sendKeyEvent(新的KeyEvent)(eventTime,eventTime,KeyEvent.ACTION\u DOWN,code,0,0, KeyCharacterMap.VIRTUAL_键盘,0,KeyEvent.FLAG_软键盘| KeyEvent.FLAG_保持触摸模式); super.sendKeyEvent(新的KeyEvent(SystemClock.uptimeMillis()、eventTime、KeyEvent.ACTION\u UP、代码、0、0、, KeyCharacterMap.VIRTUAL_键盘,0,KeyEvent.FLAG_软键盘| KeyEvent.FLAG_保持触摸模式); } }; } }; handle=new Handler();//handle在Androidput中是私有的,所以我们创建自己的。 //Android有两个不同的AndroidPut类,具体取决于SDK版本,请创建正确的一个。如果不支持API<12,可以删除其中一个。 如果(Build.VERSION.PREVIEW\u SDK\u INT>=12){ input=newandroidipthrePlus(this,this,graphics.getView(),config){ @凌驾 公共无效设置ScreenkeyBoardVisible(布尔可见){ AndroidLauncher.this.setOnscreenKeyboardVisible(可见); } }; }否则{ input=newandroidInput(this,this,graphics.getView(),config){ @凌驾 公共无效设置ScreenkeyBoardVisible(布尔可见){ AndroidLauncher.this.setOnscreenKeyboardVisible(可见); } }; } } 私有void setOnscreenKeyboardVisible(最终布尔值可见){ //代码来自AndroidPut.setOnscreenKeyboardVisible,视图更改除外。 handle.post(新的Runnable(){ 公开作废运行(){ InputMethodManager=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务); 如果(可见){ inputView.setFocusable(真); inputView.setFocusableInTouchMode(真); manager.showSoftInput(inputView,0); }否则{ manager.hideSoftInputFromWindow(inputView.getWindowToken(),0); } } }); } } 如你所见,这并不容易做到。我也没有测试过,所以可能会有更多