我制作了一个自定义键盘应用程序:
public class SimpleIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener
{
private KeyboardView kv;
private Keyboard keyboard;
private boolean caps = false;
@Override
public View onCreateInputView()
{
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.qwerty);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
return kv;
}
}
XML:
<android.inputmethodservice.KeyboardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:keyBackground="@drawable/key_image"
android:background="@drawable/keyboard_image"
android:keyPreviewLayout ="@layout/preview"
/>
现在,有一个设置键,当用户按下它时,新的Activity将打开。此 Activity 有两个按钮。第一个应该更改键盘背景图像,第二个应该更改按钮背景图像。
我正在尝试一些事情,但我无法做到。有人可以告诉我我应该怎么做吗?
谢谢。
最佳答案
我已经找到解决办法了。通过从 Activity 更改主题可以轻松更改键盘:
themePreferencesEditor.putInt(THEME_KEY, 2);
themePreferencesEditor.commit();
然后在 IME 中:
@Override
public View onCreateInputView()
{
pre = getSharedPreferences("CUSTOM_KEYBOARD_PREFERENCES", 1);
theme = pre.getInt(THEME_KEY, 1);
switch (theme)
{
case 1:
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
break;
case 2:
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard1, null);
break;
case 3:
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard2, null);
break;
case 4:
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard3, null);
break;
case 5:
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard4, null);
break;
case 6:
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard5, null);
break;
default:
kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
break;
}
keyboard = new Keyboard(this, R.xml.qwerty);
kv.setKeyboard(keyboard);
kv.setOnKeyboardActionListener(this);
return kv;
}
@Override
public void onStartInputView(EditorInfo info, boolean restarting)
{
super.onStartInputView(info, restarting);
setInputView(onCreateInputView());
}
关于Android 自定义键盘从 Activity 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29277390/