自定义SoftKeyboard视图无法正常显示

时间:2012-10-04 00:04:49

标签: android view ime

我扩展了Android Sample SoftKeyboard示例。 除了LatinKeyBoardView之外,此扩展所需要的是在键盘上方添加两个垂直AlphabetLinearLayouts(从LinearLayout扩展)。 LatinKeyboardView是标准键盘。

当我从布局中删除这两个侧视图时,行为符合预期 - 在EditText内部选择拉出软键盘,以及在向上移动之前屏幕上的所有视图。

通常当键盘弹出纵向时,屏幕上的任何内容都会移动到键盘上方,有效地将一些内容从屏幕上移开,为键盘腾出空间。

我认为,因为我将这两个额外的垂直视图作为键盘的一部分,所以框架会尝试推送内容并失败,因为键盘视图会占用太多空间。

我正在试图弄清楚如何解决这个问题。最好的情况是以某种方式让我的垂直视图显示在原始内容上,除了显示软键盘所需的位置之外没有其他位移。

我按如下方式构建我的KeyboardView:

@Override public View onCreateInputView() {
  RelativeLayout outer = (RelativeLayout) getLayoutInflater().inflate(
        R.layout.input, null);
  // Set up keyboard view.
  mInputView = (LatinKeyboardView) outer.findViewById(R.id.keyboard);
  mInputView.setOnKeyboardActionListener(this);
  mInputView.setKeyboard(mQwertyKeyboard);
  // Maintain reference to APC.
  mAlphabetViewLeft = (AlphabetColumnLayout)outer.findViewById(R.id.alphabet_list_left);
  mAlphabetViewRight = (AlphabetColumnLayout)outer.findViewById(R.id.alphabet_list_right);

  // Set of references to handle touches.
  mAlphabetViewLeft.setTouchResolver(mTouchResolver);
  mAlphabetViewRight.setTouchResolver(mTouchResolver);

  return outer;
}

我的input.xml文件如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res/com.example.android.softkeyboard">

<com.example.android.softkeyboard.AlphabetColumnLayout
    android:id="@+id/alphabet_list_left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_above="@+id/keyboard"
    android:orientation="vertical"
    app:available_letters="BGHIJKLMNOPUVY"/>

<com.example.android.softkeyboard.AlphabetColumnLayout
    android:id="@+id/alphabet_list_right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_above="@+id/keyboard"
    android:orientation="vertical"
    app:available_letters="ACDEFQRSTWXZ"/>

<com.example.android.softkeyboard.LatinKeyboardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</RelativeLayout>

最后,我使用以下代码设置AlphabetColumnLayouts。它的作用是在键盘上方的屏幕两侧创建一堆字母(我上面提到的两个输入视图)。 AlphabetTextView是一个普通的TextView,有一些额外的东西可以帮助我以我想要的方式解决这些问题。 char []字母使用来自input.xml的app:available_letters样式进行实例化。

public void instantiateViews(char[] letters, int appSize) {
  Context context = mWeakContext.get();
  assertNotNull(context);

  // Set up layout params of parent based on the screen size available,
  // keeping XML-defined params.
  RelativeLayout.LayoutParams lp =
      (RelativeLayout.LayoutParams) getLayoutParams();
  if (lp == null) {
    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
  }
  lp.height = appSize/2;
  Log.e(TAG, "LinearLayout = " + lp.debug(""));
  setLayoutParams(lp);

  // Set up the child TextViews.
  final int nLetters = letters.length;
  for (char c : letters) {
    AlphabetTextView tv = new AlphabetTextView(context, this);
    LinearLayout.LayoutParams tlp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.MATCH_PARENT,
      lp.height/nLetters);
    tv.setLayoutParams(tlp);
    //tv.setGravity(Gravity.CENTER);
    tv.setText(String.valueOf(c));
    tv.setTextSize(12);
    tv.setTypeface(Typeface.DEFAULT_BOLD);

    // Add child to Layout.
    addView(tv);
  }
  setOnTouchListener(this);
}

这是我发布的一些附加图片的Android群组问题的a link

1 个答案:

答案 0 :(得分:1)

您的描述不是很清楚。你究竟看到了什么是错的?此外,您所描述的内容听起来像您想要创建自己的候选人版本?实施onCreateCandidateViews()不符合您的需求吗?

<强>更新

查看屏幕截图,我现在明白你的意思了。

我认为你基本上是在走向痛苦的道路上。您创建的内容是KeyboardView,其大小为View,其高度包括TextView列(不足为奇)。应用程序如何与输入方法交互不取决于输入方法,而是取决于应用程序本身。在这种情况下,消息应用似乎将其android:windowSoftInputMode设置为adjustResize(因此,在定位更改后,它“坐在”KeyboardView之上 - 我不知道它为什么没有首次显示输入法时调整大小。

此外,即使您确实设法让底层应用程序运行,您也会遇到输入法可能会从应用程序本身窃取触摸事件的问题(即您的KeyboardView仍然是一个矩形,无论屏幕底部有什么透明度,它都位于屏幕的底部。

基本上,Android的IME框架适用于作为单根视图的输入方法,它粘贴在Dialog中,并在屏幕底部显示。可能是最近的框架变化已经偏离了这一点;添加了一个新的Keyboard ctor,允许客户指定宽度和高度,而不是假设Key大小百分比是屏幕宽度/高度。

无论如何,虽然有可能做其他的事情只是IME必须做应用程序所期望的,这是黄金法则,因为如果你的IME不与用户的应用程序互操作,那么他们将不会使用它