2015-11-30 96 views
1

我正在開發一個自定義鍵盤,並且想要在鍵盤上添加一個TextView以顯示用戶已輸入的內容或他想要輸入的單詞的建議。windowSoftInputMode =「adjustResize」導致自定義鍵盤佈局出現問題

爲了做到這一點,我有以下佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
    <android.inputmethodservice.KeyboardView 
     android:id="@+id/keyboard" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:keyPreviewLayout="@layout/preview" 
     android:keyBackground="@drawable/key_background" 
     android:background="@color/color_primary" 
     /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/keyboard" 
     android:padding="8dp" 
     android:gravity="center" 
     android:background="@color/color_primary_dark" 
     android:textColor="@android:color/white" 
     android:text="some sample text" 
     /> 
</RelativeLayout> 

和下面的代碼在InputMethodService

public class FancyInputMethodService extends InputMethodService { 
    @Override 
    public View onCreateInputView() { 
     final RelativeLayout layout = (RelativeLayout) getLayoutInflater().inflate(R.layout.keyboard_layout, null); 
     final KeyboardView keyboardView = (KeyboardView) layout.findViewById(R.id.keyboard); 
     final Keyboard keyboard = new Keyboard(this, R.xml.qwerty); 
     keyboardView.setKeyboard(keyboard); 
     return layout; 
    } 
} 

在正常EditText在屏幕上方的鍵盤看起來很好,效果很好:

enter image description here

但是,如果EditTextActivity中使用清單中的標誌android:windowSoftInputMode="adjustResize",則鍵盤視圖似乎覆蓋實際視圖而不是透明。

enter image description here

左邊的圖像顯示了軟鍵盤的實際視圖關閉時,中間的圖像顯示了怪異的行爲,當鍵盤打開,右邊的圖像顯示了默認的鍵盤行爲我會期待。

我已經嘗試將佈局背景設置爲透明,但這並沒有幫助。

該問題出現在多個應用程序中,例如, WhatsApp的,環聊,Facebook等...我錯過了什麼或錯在哪裏?

回答

0

TL;博士

您沒有使用InputMethodService如預期,使用CandidateView框架來代替。

完整的答案:

  1. 你的鍵盤佈局應該不包括文本建議。

  2. 重寫InputMethodService#onCreateCandidatesView。它應該是這樣的:

公共查看onCreateCandidatesView(){

mYourView = getLayoutInflater().inflate(R.layout.your_view, null); 
    return mYourView; 
} 

3.當你想顯示/隱藏候選視圖使用setCandidatesViewShown(布爾顯示)