Android长按自定义键盘弹出键盘

时间:2015-04-01 11:57:21

标签: android keyboard android-softkeyboard keyboard-events android-input-method

我有自定义Android键盘:

    public class CustomKeyboard extends Keyboard{...}  

    public class CustomKeyboardView extends KeyboardView{...}

    public class CustomKeyboardIME extends InputMethodService implements KeyboardView.OnKeyboardActionListener{...}  

在某些键上,我有popupKeyboardpopupCharacters

<Key android:codes="144" android:keyLabel="0" android:popupKeyboard="@xml/key_popup" android:popupCharacters=")" android:keyEdgeFlags="right"/>

XML / key_popup.xml:

<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
      android:keyWidth="10%p"
      android:horizontalGap="0px"
      android:verticalGap="0px"
      android:keyHeight="@dimen/key_height" >
</Keyboard>

但是当我长时间按下&#34; 0&#34;关键弹出窗口&#34;)&#34;显示,但它一直停留在那里,直到我按下&#34; X&#34;按钮或&#34;)&#34;字符。它看起来像这样:
My keyboard

我希望它只在我按住手指时打开。三星或HTC键盘上的东西:
Samsung keyboard

有人能帮助我吗?

编辑至少可以更改此弹出窗口的外观吗?我希望它具有与我制作的整个键盘相同的背景和键/

1 个答案:

答案 0 :(得分:6)

您可以使用PopupWindow类并使用自定义布局填充它。

View custom = LayoutInflater.from(context)
    .inflate(R.layout.your_layout, new FrameLayout(context));
PopupWindow popup = new PopupWindow(context);
popup.setContentView(custom);

并长按

//Get x,y based on the touch position
//Get width, height based on your layout
if(popup.isShowing()){
    popup.update(x, y, width, height);
} else {
    popup.setWidth(width);
    popup.setHeight(height);
    popup.showAtLocation(yourKeyboardView, Gravity.NO_GRAVITY, x, y);
}

点击弹出窗口,你可以将其解雇

popup.dismiss();