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

5

我已经成功实现了自定义键盘。它正在按照预期工作。我想在键盘顶部添加一个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"
/>
< p > 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);
        }
    }

如果您投反对票,请留下评论。

如果您觉得这篇文章对您有所帮助,请标记它为已接受的答案。 - Prasad Pawar
@ user5596252,您找到了解决方案吗?如果是,请帮助我。 - Ashish Agrawal
1个回答

5
当然可以!
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;
} 

完成。

我尝试了解决方案,但是没有成功,出现以下错误:进程:com.mykeyboard,PID:16969 java.lang.IllegalStateException:指定的子项已经有一个父项。您必须首先在子项的父项上调用removeView()。 在android.view.ViewGroup.addViewInner(ViewGroup.java:4915)中。 - Ashish Agrawal
@AshishAgrawal请在新的SO问题中打开您的代码片段并提供链接。您所提到的问题完全是关于其他事情的。 - Prasad Pawar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接