我创建了一个自定义键盘,用户可以在其中启用新的输入法。但是,我想检测他们是否启用了它。我使用了以下代码:
Intent enableIntent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(enableIntent);
但是,如果用户启用了我自定义创建的键盘,那么我想更改程序的UI,如果不更改,则什么也不做。我知道我可以从这里获取已启用的UI列表:
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
String list = imm.getEnabledInputMethodList().toString();
这不符合预期。这是我的功能
private Boolean checkIfKeyboardIsSelected() {
Intent enableIntent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(enableIntent);
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
String list = imm.getEnabledInputMethodList().toString();
Log.e("List", list);
if(list.contains(KEYBOARDID))
{
return true;
}
return false;
}
我该怎么办?
答案 0 :(得分:1)
您可以这样:
//call this line in some place
startActivityForResult(new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS),1);
并覆盖onActivityResult
:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
String list = imm.getEnabledInputMethodList().toString();
if (requestCode == 1) {
if (list.contains(KEYBOARDID)) {
Toast.makeText(this, "good", Toast.LENGTH_SHORT).show();
//do what you want
} else {
Toast.makeText(this, "bad", Toast.LENGTH_LONG).show();
}
}
}