0

问题陈述

我正在尝试按照示例“softkeyboard”实现我自己的虚拟键盘,该示例位于 android-sdk 的示例文件夹中。

在 onCreateInputView() 视图中使用布局充气器创建,如下所示:

@Override
public View onCreateInputView() {
    mContainerView = getLayoutInflater().inflate(R.layout.my_keyboard, null);
    return mContainerView;
}

在我漂亮的虚拟键盘 gui 定义 (my_keyboard.xml) 中,我添加了一些像这样的按钮。

<Button
    android:id="@+id/my_button1"
    android:onClick="onMyButton1Pressed"
    android:text="@string/my_button1_text" />

现在,如果我运行程序,Button 似乎正在工作: onMyButton1Pressed() 被调用 OK。问题是按钮状态图(按下时橙色突出显示)不起作用。

编辑 1:问题与 ToggleButton 相同:按下时,不会出现橙色突出显示。但是绿色的“选中”标记有效。如果我在正常活动中使用这些,按下按钮时会出现橙色突出显示。

到目前为止,我的发现可能有用或没有......

我一直在尝试 google,并阅读 android 文档。在某处我读到使用带有空根元素的膨胀,不会将主题/样式/背景可绘制/任何东西(正确的术语是什么?)应用于膨胀的层次结构。这是问题所在,为什么按钮状态不显示?

现在当我创建虚拟键盘时,根元素是什么,在哪里添加了键盘?

我还找到了另一种充气方法:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

    resource = ID for an XML layout resource to load (e.g., R.layout.main_page)
    root = Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
    attachToRoot = Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

我可以通过它可以从中提取布局参数的一些“虚拟”视图组吗?

4

1 回答 1

0

我按照芒果的建议尝试了背景可绘制+选择器xml,它可以工作。因此,这是一种可能的解决方法:

1.添加背景可绘制按钮

<Button
    android:id="@+id/my_button1"
    android:onClick="onMyButton1Pressed"
    android:text="@string/my_button1_text" 
    android:background="@drawable/my_button_drawable
/>

2.在/res/drawable/中创建my_button_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/btn_default_pressed" />
    <item android:state_enabled="false"
          android:drawable="@drawable/btn_default_transparent_normal" />
    <item android:drawable="@drawable/btn_default_normal" />
</selector>

3.寻找按钮背景图片:ANDROID_SDK_FOLDER/platforms/android-8/data/res/drawable-hdpi/

  • btn_default_normal.9.png(用于正常状态)
  • btn_default_pressed.9.png(用于按下状态)
  • btn_default_transparent_normal.9.png(用于禁用状态)

并将这些图像添加到可绘制文件夹中。

现在,如果我按下按钮,它会变成橙色突出显示,并且禁用状态看起来也不错。我不需要焦点或选定状态,因此我没有将它们包含在选择器中。

于 2012-10-09T08:48:48.610 回答