Change Keyboard Input Language Programmatically

后端 未结 2 2110

I am developing an application in which i need to allow the user to change the input keys shown in the default keyboard, upon request or by default, for example, i may promp

2条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 15:53

    You can change keyboard without user notification only and only if your app is running as a System app for security reasons.

    You need to give Android permission first in your app's AndroidManifest.xml

    "android.permission.WRITE_SECURE_SETTINGS"
    

    Then you need to determine id of your keyboard.

    -> To know id, you need to keep your keyboard default from setting menu manually then put this print somewhere,

    System.out.println(Settings.Secure.getString(getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD));
    

    Once you print id and you know your keyboard id you can do as per below ( I have changed my default keyboard to Japanese )

    InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
    
    //imeManager.showInputMethodPicker(); //This is to see available keyboards.
    imeManager.setInputMethod(null,"jp.co.omronsoft.openwnn/.OpenWnnJAJP");
    

    Enjoy !!

提交回复
热议问题