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

Posted

技术标签:

【中文标题】Android:在自定义键盘上添加 imageButton【英文标题】:Android: Add imageButton on top of a custom keyboard 【发布时间】:2016-03-03 02:13:56 【问题描述】:

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

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

Keyboard.xml 键盘布局

<android.inputmethodservice.KeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_
    android:layout_
    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);
        
    

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

【问题讨论】:

如果对您有帮助,请将答案标记为已接受。 @user5596252 你有没有找到任何解决方案,如果有请帮助我 【参考方案1】:

当然!

1) 更改keyboard.xml 如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <RelativeLayout
        android:layout_
        android:layout_
        >

        <ImageView
            android:id="@+id/ivKeyboard"
            android:layout_
            android:layout_
            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_
        android:layout_
        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) @AshishAgrawal 使用您的代码 sn-p 打开一个新的 SO 问题并提供链接。您所指的问题完全是另一回事。

以上是关于Android:在自定义键盘上添加 imageButton的主要内容,如果未能解决你的问题,请参考以下文章

如何防止键盘出现在自定义警报视图上

在自定义中从 TextField 中关闭键盘

在自定义数字键盘上的按键上点击播放声音

ios) 如何在自定义键盘上方创建建议视图

Android:在自定义视图上使用android绑定点击事件

在自定义适配器中隐藏软键盘