当指定列限制时,android中的弹出键盘问题

时间:2012-02-19 15:52:58

标签: android keyboard popupwindow

我正在研究android的输入法,其中一项任务是为硬键盘按键实现自定义弹出键盘。通常输入方法让编辑器处理,但事情是我需要添加比android支持更多的符号。 所以我实现了弹出键盘,甚至在长按硬键(字符键)时显示效果很好。

我遵循的步骤是:

  1. 创建弹出窗口。
  2. 对包含键盘视图和关闭按钮的线性布局进行充气,并将其保存到视图对象
  3. 将键盘视图和关闭按钮绑定到相关对象
  4. 为弹出字符创建键盘,并将其设置为键盘视图的键盘。键盘的列限制为5.
  5. 将线性布局设置为弹出窗口的内容视图
  6. 显示弹出窗口
  7. 问题:如果弹出键盘中有多行,我只能在列的最后一行中选择键。即使我单击该列第一行中的键,也会选中最后一行中的键。 如果有人能解释为什么会发生这种情况以及如何解决它,我很感激。

    代码:

    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 个答案:

答案 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) 

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

方法在此语句中将desired 200添加到desiredHeight:

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

注意这将导致motionEvent.getRawY()为高度增加200p工作