2015-11-30 92 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(布尔显示)