2012-02-19 53 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高度工作