为Android创建自定义键盘 [英] Creating custom keyboard for android

查看:113
本文介绍了为Android创建自定义键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,您可能会看到一些诸如此类的问题. 但是阅读所有这些问题/答案以及Google中几乎所有的android输入法网页,我仍然遇到麻烦.

Well, you might have seen few questions like asking for this. But reading all of those questions/answers and almost all of the android inputmethod webpages in Google, I am still in trouble.

我的最终目标是创建一个自定义键盘.但是,当然,我的某些语言将有特殊的输入法.

My final goal is to create a custom keyboard. But of course, mine will have special input methods for certain language.

但是这次,我只想在弹出键盘时显示我的自定义视图.我已经设法基于qwerty.xml文件弹出默认布局,就像这样.

But this time, all I want is to show my custom view when the keyboard is popped up. I've managed to pop the default layout based on a qwerty.xml file, which is something like this.

xml/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的类中,我有这段代码可以创建输入视图.

And in my class which extends InputMethodService, i have this code which creates input view.

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中的设置"键时,

and of course, because I had problems on applying my custom view to my source. I just made some effort on the other side. When Settings key in xml is pressed,

<Key android:codes="999" android:keyLabel="Settings" android:keyWidth="20%" android:keyEdgeFlags="left"/>

它将调用SettingsActivity

it will call 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);
}

到这里为止都很好用,但是我遇到了这个视图问题.

It works nicely until here, but I am in to this view problem.

通过阅读android开发者和一些文章,我的想法是,我可以扩展KeyboardView来创建我的自定义视图,并且通过这种方法,我也许可以使用onDraw()来绘制键.但是我在执行此操作时遇到了很多麻烦.

What I think from reading android develpers and some of the articles is that, I could extend KeyboardView to make my custom view and in this method, I might be able to draw keys somehow with onDraw(). But I am having so much trouble on doing this.

任何建议都会使您满意.谢谢.

Any suggestions will pleased. Thanks.

推荐答案

要自定义键盘,您需要修改xml文件夹中的"qwerty"和布局文件夹中的键盘.我将显示一些示例:

To customize the keyboard you will need to modify your "qwerty" in the xml folder and the keyboard on the layout folder. I'll show some examples:

这在布局文件夹上

<?xml version="1.0" encoding="UTF-8"?>
<com.example.keyboard.MyKeyboardView
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:keyPreviewLayout="@layout/preview"
android:keyBackground="@drawable/key_selector"
android:shadowRadius="0.0"
android:keyTextColor="#000000"
/> 

这在xml文件夹中进行:

And this goes on the xml folder:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="10%p"
android:keyHeight="8%p">

<Row android:verticalGap="1%p"  android:horizontalGap="0.5%p" android:keyWidth="9.6%p">
    <Key android:codes="113"    android:keyLabel="q" />
    <Key android:codes="119"    android:keyLabel="w" />
    <Key android:codes="101"    android:keyLabel="e" />
    <Key android:codes="114"    android:keyLabel="r" />
    <Key android:codes="116"    android:keyLabel="t" />
    <Key android:codes="121"    android:keyLabel="y" />
    <Key android:codes="117"    android:keyLabel="u" />
    <Key android:codes="105"    android:keyLabel="i"  />
    <Key android:codes="111"    android:keyLabel="o"  />
    <Key android:codes="112"    android:keyLabel="p" />
</Row>
<Row android:verticalGap="1%p" android:horizontalGap="0.5%p" android:keyWidth="9.6%p">
    <Key android:codes="97"    android:keyLabel="a" android:keyEdgeFlags="left" android:horizontalGap="5%p" />
    <Key android:codes="115"    android:keyLabel="s" />
    <Key android:codes="100"    android:keyLabel="d" />
    <Key android:codes="102"    android:keyLabel="f" />
    <Key android:codes="103"    android:keyLabel="g" />
    <Key android:codes="104"    android:keyLabel="h" />
    <Key android:codes="106"    android:keyLabel="j" />
    <Key android:codes="107"    android:keyLabel="k" />
    <Key android:codes="108"    android:keyLabel="l" />
</Row>
<Row android:verticalGap="1%p"  android:horizontalGap="0.5%p" android:keyWidth="9.6%p">
    <Key android:codes="3"      android:keyIcon="@drawable/keyboard_shift_off_normal" 
         android:keyWidth="13.7%p"/>
    <Key android:codes="122"    android:keyLabel="z" android:horizontalGap="1%p"/>
    <Key android:codes="120"    android:keyLabel="x" />
    <Key android:codes="99"     android:keyLabel="c" />
    <Key android:codes="118"    android:keyLabel="v" />
    <Key android:codes="98"     android:keyLabel="b" />
    <Key android:codes="110"    android:keyLabel="n" />
    <Key android:codes="109"    android:keyLabel="m" />
    <Key android:codes="-5"     android:keyIcon="@drawable/sym_keyboard_delete_dim"
         android:keyWidth="13.7%p" 
        android:horizontalGap="1%p"/>
</Row>
<Row android:verticalGap="1%p"  android:horizontalGap="0.5%p" android:keyWidth="9.6%p">
    <Key android:codes="-16"    android:keyIcon="@drawable/keyboard_symbol"
         android:keyWidth="18.7%p"/>
    <Key android:codes="44"     android:keyLabel="," android:horizontalGap="1%p"/>
    <Key android:codes="32"     android:keyIcon="@drawable/sym_keyboard_feedback_space" android:keyWidth="40%p"/>
    <Key android:codes="46"     android:keyLabel="."/>
    <Key android:codes="-3"     android:keyIcon="@drawable/keyboard_go" 
         android:keyWidth="18.5%p" android:horizontalGap="1%p"/>
</Row>
</Keyboard>

就像您说的那样,您可以从键盘布局扩展一个类,但是它更多地用于自定义键盘事件,例如onTouch,onLongKeyPress,

Like you said, you can extend a class from keyboard layout, but it's more for customize keyboard events, like onTouch, onLongKeyPress,

这篇关于为Android创建自定义键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆