【发布时间】:2017-07-12 20:46:22
【问题描述】:
我目前正在开发一个可以通过软键盘接收用户输入的 Android 应用程序。但是当我的键盘出现时出现了一些问题,当我使用下面的代码时只有英文键盘:
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
而且谷歌输入法显示没有我的默认语言(这意味着我无法切换到其他语言,因为没有选项卡可供选择),但我搜索整篇文章,似乎没有办法通过代码更改 IME 语言。
我使用 LIBGDX,它的 ui 元素 (TextField) 或从 Android 直接调用它都无法以我的默认语言 (中文) 显示 Google IME。但如果我使用系统 IME,它可以按预期工作。
为了明确其他问题,我可以在其他应用程序中使用谷歌输入法没有任何问题,并且我还发布了我用来预先调用 InputMethodManager 的代码。
package my.package;
import android.app.Service;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Handler;
import android.os.LocaleList;
import android.os.Looper;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.EditText;
import java.util.Locale;
import my.package.Debugger;
public final class AndroidKeyboardAdapter implements KeyboardAdapter{
InputMethodManager imm;
Context context;
EditText text;
public AndroidKeyboardAdapter(Context context) {
this.context = context;
imm = (InputMethodManager)context.getSystemService(Service.INPUT_METHOD_SERVICE);
}
public void showKeyboard() {
text = new EditText(context);
Locale locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = context.getResources().getConfiguration().getLocales().get(0);
for(int i = 0; i < context.getResources().getConfiguration().getLocales().size();i++)
Debugger.LogInConsole(context.getResources().getConfiguration().getLocales().get(i).getCountry()); // show TW
} else{
//noinspection deprecation
locale = context.getResources().getConfiguration().locale;
Debugger.LogInConsole(locale.getCountry()); // not doing anything
}
Debugger.LogInConsole(Locale.getDefault().toString()); //show zh_tw
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public void hideKeyboard() {
imm.hideSoftInputFromWindow(text.getWindowToken(), 0);
}
public void onResume() {
text.requestFocus();
text.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
imm.showSoftInput(text, 0);
}
},200);
}
@Override
public String getText() {
return text.getText().toString();
}
}
同时提供LIBGDX在IMM上处理的源代码:
@Override
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(((AndroidGraphics)app.getGraphics()).getView(), 0);
} else {
manager.hideSoftInputFromWindow(((AndroidGraphics)app.getGraphics()).getView().getWindowToken(), 0);
}
}
});
}
【问题讨论】: