Android:在自定义键盘上添加 imageButton

标签 android input keyboard ime custom-keyboard

我已经成功实现了自定义键盘。它正在正常工作。我想在键盘顶部添加一个 imageButton 如下图所示,这样每当键盘弹出时它总是显示在键盘上方。谁能指导我如何将此图像按钮添加到我的自定义键盘?

Screenshot of keyboard

下面是键盘的代码,如果有人想看的话。

Keyboard.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:keyPreviewLayout ="@layout/preview"
/>

keyPreviewLayout 是短暂弹出窗口的布局,只要按下键盘上的某个键就会显示。

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>
        <Key android:codes="49" android:keyLabel="1" android:keyEdgeFlags="left"/>
        <Key android:codes="50" android:keyLabel="2"/>
        <Key android:codes="51" android:keyLabel="3"/>
        <Key android:codes="52" android:keyLabel="4"/>
        <Key android:codes="53" android:keyLabel="5"/>
        <Key android:codes="54" android:keyLabel="6"/>
        <Key android:codes="55" android:keyLabel="7"/>
        <Key android:codes="56" android:keyLabel="8"/>
        <Key android:codes="57" android:keyLabel="9"/>
        <Key android:codes="48" android:keyLabel="0" android:keyEdgeFlags="right"/>
    </Row>
    <Row>
        <Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/>
        <Key android:codes="119" android:keyLabel="w"/>
        <Key android:codes="101" android:keyLabel="e"/>
<!--And so on for all the keys-->

SimpleIME.java 这是键盘的服务类

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;
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();
//        playClick(primaryCode);
        switch(primaryCode){
            case Keyboard.KEYCODE_DELETE :
                ic.deleteSurroundingText(1, 0);
                break;
            case Keyboard.KEYCODE_SHIFT:
                caps = !caps;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char)primaryCode;
                if(Character.isLetter(code) && caps){
                    code = Character.toUpperCase(code);
                }
                ic.commitText(String.valueOf(code),1);
        }
    }

如果您投反对票,请发表评论。

最佳答案

当然可以!

1) 改变 keyboard.xml 如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <ImageView
            android:id="@+id/ivKeyboard"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_alignParentLeft="true"
            android:padding="10dp"
            android:scaleType="fitEnd"
            android:src="@drawable/keyboard_icon" />

    </RelativeLayout>

    <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:keyPreviewLayout ="@layout/preview"
    />

</LinearLayout>

2) 在 SimpleIME.java 中做一些修改:

@Override 
public View onCreateInputView() {

    final View root = getLayoutInflater().inflate(R.layout.idee_keyboard_layout, null);

    ImageView ivKeyboard = (ImageView) root.findViewById(R.id.ivRevertKeyboard);

    ivKeyboard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //whatever you want to do...
        }
    });

    kv = (KeyboardView) root.findViewById(R.id.keyboard);

    keyboard = new Keyboard(this, R.xml.qwerty);
    kv.setKeyboard(keyboard);
    kv.setOnKeyboardActionListener(this);
    return root;
} 

完成。

关于Android:在自定义键盘上添加 imageButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33981731/

相关文章:

python - 重新配置鼠标设备

android通过网络加密json数据

android - 自定义 XML 资源中的引用字符串资源

android - Facebook android 集成中的注销功能禁用

windows - 是否可以通过编程方式禁用 Windows 上的 Caps Lock 键?

iphone - 在正确的时间显示键盘 iOS7

ios - 禁用 UITextField 键盘中的麦克风按钮

Android Studio 可以正确地将应用程序与 -lGLESv3 链接到 64 位arm,但不能链接到 32 位arm

jquery - 为什么 jQuery val() 函数不更新属性 'value' ?

python - int 的文字无效 - 从 CSV 文件获取原始数据