0
我目前正在開發一款可以通過軟鍵盤接收用戶輸入的Android應用程序。但有一些問題,當我的鍵盤顯示,只有英文鍵盤,當我使用下面的代碼:Android中的Google IME
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
而谷歌輸入法顯示沒有我的默認語言(這意味着我不能切換到其他語言因爲沒有選項卡),但我搜索整篇文章,似乎沒有辦法通過代碼更改IME語言。
我使用的LIBGDX,既沒有它的UI元素(TextField),也沒有從Android調用direclty可以用我的默認語言(中文)顯示Google IME。但是如果我使用系統IME,它可以按預期工作。
如果還有其他問題,我可以在其他應用程序中使用Google 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();
}
}
另外提供的源代碼上的IMM該LIBGDX過程:
@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);
}
}
});
}