Android的自定义键盘如何进行弹出式键盘上长按?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (22)

我有自定义的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>

但是,当我长按“0”键弹出与“)”显示,但它保持在那里,直到我按下“X”按钮或“)”字符。它看起来像这样:

我只希望它能在我手指上打开。像三星或HTC键盘上的东西:

提问于
用户回答回答于

你可以使用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();

扫码关注云+社区