如何在安卓键盘服务中访问从 XML 加载的 IME 首选项



我正在尝试为 Android 创建自己的自定义软件键盘。我从本教程中启发了自己,所以我的键盘服务 onCreateInputView(( 如下所示:

...
private KeyboardView keyboardView;
private Keyboard defaultKeyboard;
private Keyboard charKeyboard;
...
@Override
public View onCreateInputView()
{
    defaultKeyboard = new Keyboard(this, R.xml.alphabet_letter_keyboard);
    charKeyboard = new Keyboard(this, R.xml.specialcharacter_keyboard);
    keyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.keyboard, null);
    keyboardView.setKeyboard(defaultKeyboard);
...

我还查看了 AOSP 键盘实现键盘首选项的方式(在 Android 设置 -> 输入法中,如果您单击键盘名称,您将到达键盘设置屏幕(,所以我从 XML 文件进行设置,如下所示:

public static class Settings extends InputMethodSettingsFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setInputMethodSettingsCategoryTitle(R.string.layout_selection_title);
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.ime_preferences);
...

XML 设置如下所示:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="@string/select_layout">
        android:title="Layouts"
        android:key="layouts"
        android:summary="Click to select the keyboard layout"
        android:defaultValue="qwertz"
        android:entries="@array/list"
        android:entryValues="@array/values"
        ></ListPreference>
</PreferenceCategory>

所以我可以从不同的布局中选择设置(qwerty,字母顺序......我想实现的是访问键盘服务onCreateInputView((方法中的选定值,因此将为defaultKeyboard实例采用正确的布局。

例如

@Override
public View onCreateInputView()
{        
    // Access the list setting value of the selected layout
    String selectedLayout = ...
    switch(selectedLayout) { // Initialize keyboard with abcde or qwert layout
        case "abcde":
            defaultKeyboard = new Keyboard(this, R.xml.alphabet_letter_keyboard);
            break;
        case "qwert":
            defaultKeyboard = new Keyboard(this, R.xml.typewriter_letter_keyboard);
            break;
    }
...

我找到了解决方案。您可以通过以下方式访问设置

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.getString("layouts", "qwert")

prefs.getString 的第二个参数是默认值。要在更改设置时更改键盘布局,您需要实现共享首选项更改的方法

一般代码如下所示:

public class KeyboardService extends InputMethodService implements KeyboardView.OnKeyboardActionListener, SharedPreferences.OnSharedPreferenceChangeListener
{
private KeyboardView keyboardView;
private Keyboard defaultKeyboard;
private Keyboard charKeyboard;
private boolean isCaps = true; //Start the keyboard in Caps-layout
private boolean isCharKeyboard = false; //Start the keyboard with letter-layout
SharedPreferences prefs;
@Override
public View onCreateInputView()
{
    // SharedPreferences prefs = getSharedPreferences("ime_preferences", MODE_PRIVATE);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    // register preference change listener
    prefs.registerOnSharedPreferenceChangeListener(this);
    // Set the initial layout according to the initial layout selection
    switch(prefs.getString("layouts", "qwert")) { // Initialize keyboard with abcde or qwert layout
        case "abcde":
            defaultKeyboard = new Keyboard(this, R.xml.alphabet_letter_keyboard);
            break;
        case "qwert":
            defaultKeyboard = new Keyboard(this, R.xml.typewriter_letter_keyboard);
            break;
    }
...
// handle updates to preferences
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    if (key.equals("layouts")) {
        switch(prefs.getString("layouts", "qwert")) { // Initialize keyboard with abcde or qwert layout
        case "abcde":
            defaultKeyboard = new Keyboard(this, R.xml.alphabet_letter_keyboard);
            break;
        case "qwert":
            defaultKeyboard = new Keyboard(this, R.xml.typewriter_letter_keyboard);
            break;
    }
    keyboardView.setKeyboard(defaultKeyboard);
    setInputView(this.onCreateInputView());
    }
}

相关内容

  • 没有找到相关文章

最新更新