【发布时间】:2016-12-10 14:54:57
【问题描述】:
好吧,您可能已经见过很少有人问这个问题了。 但是阅读所有这些问题/答案以及 Google 中几乎所有的 android inputmethod 网页,我仍然遇到了麻烦。
我的最终目标是创建一个自定义键盘。当然,我的某些语言会有特殊的输入法。
但这一次,我只想在弹出键盘时显示我的自定义视图。我已经设法根据 qwerty.xml 文件弹出默认布局,类似于这样。
xml/qwerty.xml
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:horizontalGap="0px"
android:verticalGap="0px"
android:keyHeight="60dp"
>
<Row android:rowEdgeFlags="bottom">
<Key android:codes="999" android:keyLabel="Settings" android:keyWidth="20%" android:keyEdgeFlags="left"/>
<Key android:codes="44" android:keyLabel="," android:keyWidth="7%p" />
<Key android:codes="47" android:keyLabel="/" android:keyWidth="7%p" />
<Key android:codes="32" android:keyLabel="SPACE" android:keyWidth="30%p" android:isRepeatable="true"/>
<Key android:codes="-5" android:keyLabel="DEL" android:keyWidth="18%p" android:isRepeatable="true"/>
<Key android:codes="-4" android:keyLabel="DONE" android:keyWidth="18%p" android:keyEdgeFlags="right"/>
</Row>
在我扩展 InputMethodService 的类中,我有这段代码可以创建输入视图。
private KeyboardView myKeyView;
private Keyboard keyboard;
@Override
public View onCreateInputView() {
myKeyView = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
keyboard = new Keyboard(this, R.xml.qwerty);
myKeyView.setKeyboard(keyboard);
myKeyView.setOnKeyboardActionListener(this);
return myKeyView;
}
当然,因为我在将自定义视图应用于源代码时遇到了问题。 我只是在另一边做了一些努力。当按下xml中的设置键时,
<Key android:codes="999" android:keyLabel="Settings" android:keyWidth="20%" android:keyEdgeFlags="left"/>
它会调用 SettingsActivity
@Override
public void onKey(int primaryCode, int[] keyCodes) {
if(primaryCode == 999) {
openSettings();
}
}
public void openSettings()
{
Intent intent = new Intent(this, WRKeySettings.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
到这里为止它工作得很好,但我遇到了这个视图问题。
通过阅读 android 开发人员和一些文章,我的想法是,我可以扩展 KeyboardView 以制作我的自定义视图,并且在这种方法中,我可以使用 onDraw() 以某种方式绘制键。但是我在这样做时遇到了很多麻烦。
任何建议都会很高兴。谢谢。
【问题讨论】:
-
只是为了清楚地理解问题,您想以编程方式创建另一个布局吗?您想对现有的进行更改吗?您想在不同的应用程序上重复使用相同的“键盘”吗?您可以“重绘”键,但它们将具有相同的功能,或者您可以在布局中添加视图,并“重做”窗口......尽量具体说明您希望完成的工作
标签: android keyboard android-custom-view android-softkeyboard android-input-method