2012-02-19 50 views
2

我正在为android的输入法工作,其中一项任务是为硬键盘按键实现自定义弹出式键盘。通常输入法让编辑器处理,但事情是我需要添加比android支持更多的符号。 因此,我实现了弹出式键盘,并且在长时间按下硬键(字符键)时甚至可以很好地显示它。弹出键盘问题在Android中,当指定列限制时

我遵循的步骤是:

  1. 创建弹出窗口。
  2. 充气含有keyboardview和关闭按钮的线性布局并将其保存到一个视图对象
  3. 绑定的keyboardview和关闭按钮的相关对象
  4. 创建键盘弹出字符,并将其设置为键盘视图的键盘。键盘具有5.
  5. 一列的限制中的线性布局作为弹出窗口
  6. 显示弹出窗口

问题的内容视图:如果在弹出键盘的多个行我我只能在列的最后一行中选择键。即使我点击该列第一行的键,最后一行中的键也会被选中。 如果有人能解释为什么会发生这种情况,我该如何解决它,Id感谢。

的代码:

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); 
} 

回答

2

我有弹出式键盘类似的问题。我发现这只是Android 2.3的一个问题。我唯一的解决方法是避免使用多于一行的弹出式键盘。

0

发生这种情况的原因是因为KeyboardView发送了MotionEvent。 MotionEvent.getRawX()和getRawY()只返回KeyboardView边界内的坐标。如果MotionEvent发生在KeyboardView上方,它将返回KeyboardView中最接近的绝对坐标。

一个解决方案是在KeyboardView上方创建一个不可见的视图。它将不得不检测MotionEvent,然后将MotionEvent传递回KeyboardView,然后您的多行弹出式键盘将工作

有关开始代码,请查看KeyboardView上方的CandidateView。例如看这个项目: https://github.com/blackcj/AndroidCustomKeyboard

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 

https://github.com/blackcj/AndroidCustomKeyboard/blob/master/app/src/main/java/com/blackcj/customkeyboard/CandidateView.java

方法添加200在此声明desiredHeight:

setMeasuredDimension(measuredWidth, resolveSize(desiredHeight, heightMeasureSpec)); 

注意如何这会导致motionEvent.getRawY()为一个额外的200p高度工作