Programmatically enabling a custom keyboard

夙愿已清 提交于 2021-02-20 09:08:21

问题


I've developed custom keyboard on android, and I want to add a button to enable my keyboard and redirect the user to the page of virtual keyboards or to page manage keyboards so that the user can just turn on my keyboard from there, for example similar to the one in "a.i.typekeyboard", kindly see the screenshot.


回答1:


Just open input method settings activity using Intent.

Intent enableIntent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
this.startActivity(enableIntent);   



回答2:


well there is two solution , if you want to do this fully automaized you need to get access to control the mobile keybored , if you have access you will be doing this automaticly , through this code

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

other wise you can switch him to the setting keybored profile through this code

InputMethodManager imeManager = 
(InputMethodManager) 

getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();

here is some function which can help you with your application development to deal with the keyboreds setup :

**//get the old default keyboard in case you want to** 
use it later, or keep it enabled
String oldDefaultKeyboard = 
Settings.Secure.getString(resolver, 
Setting.Secure.DEFAULT_INPUT_METHOD);

**//enable your keyboard without user permission**
Settings.Secure.putString(resolver, 
Settings.Secure.ENABLED_INPUT_METHODS, 
"com.my.keyboard/.full.path");

**//set your keyboard as the new default keyboard without user permission**
Settings.Secure.putString(resolver, 
Settings.Secure.DEFAULT_INPUT_METHOD, 
"com.my.keyboard/.full.path");

//restore the keybored to default setting back through this code

@Override public void onConfigurationChanged(Configuration newConfig) {

int currentKeyboard = 0;
boolean isShifted = kv.isShifted();
for (int i = 0; i < keyboard.length; i++) {
    if(kv.getKeyboard().equals(keyboard[i])){
        currentKeyboard = i;
        break;
    }
}

super.onConfigurationChanged(newConfig);

initializeKeyboardArray();
setKeyboard(keyboard[currentKeyboard]);
kv.setShifted(isShifted);

}

private void initializeKeyboardArray(){
keyboard = new Keyboard[7];
keyboard[ARABIC] = arabic;
keyboard[ARABIC_SHIFT] = arabicShift;
keyboard[ARABIC_SYMBOLS] = arabicSymbols;
keyboard[ARABIC_SYMBOLS_SHIFT] = arabicSymbolsShift;
keyboard[QWERTY] = qwerty;
keyboard[QWERTY_SYMBOLS] = qwertySymbols;
keyboard[QWERTY_SYMBOLS_SHIFT] = qwertySymbolsShift;
}



回答3:


 Intent enableIntent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
                                enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(enableIntent);


来源:https://stackoverflow.com/questions/52126044/programmatically-enabling-a-custom-keyboard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!