我正在研究android的输入法,其中一项任务是为硬键盘按键实现自定义弹出键盘。通常输入方法让编辑器处理,但事情是我需要添加比android支持更多的符号。 所以我实现了弹出键盘,甚至在长按硬键(字符键)时显示效果很好。
我遵循的步骤是:
问题:如果弹出键盘中有多行,我只能在列的最后一行中选择键。即使我单击该列第一行中的键,也会选中最后一行中的键。 如果有人能解释为什么会发生这种情况以及如何解决它,我很感激。
代码:
PopupWindow mPopupKeyboard = new PopupWindow(this.getBaseContext());
mPopupKeyboard.setBackgroundDrawable(null);
if(mPopupKeyboard != null)
{
this.dismissPopupKeyboard();
View mMiniKeyboardContainer = null;
KeyboardView mMiniKeyboard = null;
View closeButton = null;
mMiniKeyboardContainer = getLayoutInflater().inflate(R.layout.keyboard_popup_keyboard, null);
mMiniKeyboard = (KeyboardView) mMiniKeyboardContainer.findViewById(R.id.popup_keyboardView);
closeButton = mMiniKeyboardContainer.findViewById(R.id.closeButton);
if (closeButton != null)
{
closeButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
mPopupKeyboard.dismiss();
});
}
mMiniKeyboard.setOnKeyboardActionListener(this);
String resourcestring = "abcdefghi";
mMiniKeyboard.setKeyboard(new Keyboard(this.getBaseContext(), R.xml.kbd_popup_template, alternates, 3, 0));
mMiniKeyboard.setPopupParent(mCandidateView);
mPopupKeyboard.setContentView(mMiniKeyboardContainer);
mPopupKeyboard.setWidth(LayoutParams.WRAP_CONTENT);
mPopupKeyboard.setHeight(LayoutParams.WRAP_CONTENT);
mPopupKeyboard.showAtLocation(mCandidateView, Gravity.TOP, 0, 0);
}
答案 0 :(得分:2)
我的弹出键盘有类似的问题。我发现这只是Android 2.3的问题。我唯一的解决方法是避免弹出键盘有多行。
答案 1 :(得分:0)
发生这种情况的原因是因为KeyboardView发送了MotionEvent。 MotionEvent.getRawX()和getRawY()仅返回KeyboardView边界内的坐标。如果MotionEvent发生在KeyboardView上方,它将返回KeyboardView中最接近的绝对坐标。
一种解决方案是在KeyboardView上方创建一个不可见的视图。它必须检测MotionEvent,然后将MotionEvent传递回KeyboardView,然后你的多行弹出键盘才能工作
有关起始代码,请查看KeyboardView上方的CandidateViews。例如,看看这个项目: https://github.com/blackcj/AndroidCustomKeyboard
在
中 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
方法在此语句中将desired 200添加到desiredHeight:
setMeasuredDimension(measuredWidth, resolveSize(desiredHeight, heightMeasureSpec));
注意这将导致motionEvent.getRawY()为高度增加200p工作