AutocompleteTextview 和 CompletionHintView

发布于 2024-09-17 11:36:56 字数 243 浏览 0 评论 0原文

我目前正在使用 AutocompleteTextView 组件实现自动完成字段。

我正在尝试添加带有结果数量的完成提示,并且只想将其样式设置为与下拉列表元素不同。 组件上有一个名为 completionHintView 的属性,但每次我给它一个之前定义的布局时,它都会抛出 NullPointerException

有人有关于如何设置完成提示样式的工作示例吗?

I'm currently implementing an autocomplete field by using the AutocompleteTextView component.

I'm trying to add a completion hint with the number of results, and just want to style it differently from the dropdown list element.
There is an attribute named completionHintView on the component, but every time I give it a layout I previously defined it throws a NullPointerException.

Does anyone have a working example on how to style the completion hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2024-09-24 11:36:56

您能详细说明一下您想要应用哪种样式吗?

如果只是基本的文本样式,您可能可以构建一个 Spannable 并使用结果设置完成提示,因为它接受 CharSequence 。 这篇文章

如果您正在寻找一种实际操作 TextView 参数的方法(例如填充),AutoCompleteTextView 的源代码 似乎提供了提示(双关语)。

private View getHintView(Context context) {
    if (mHintText != null && mHintText.length() > 0) {
        final TextView hintView = (TextView) LayoutInflater.from(context).inflate(
                mHintResource, null).findViewById(com.android.internal.R.id.text1);
        hintView.setText(mHintText);
        hintView.setId(HINT_VIEW_ID);
        return hintView;
    } else {
        return null;
    }
}

这表明 Android 在您指定的资源引用中查找 id text1。这种资源的最基本版本只包含具有此 id 的 TextView

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:padding="10dp"
        android:textColor="#FF0000" android:textSize="16sp" />

将上面的内容保存在布局文件中(例如 completion_hint_view.xml)并按如下所示引用它AutoCompleteTextView

<AutoCompleteTextView android:id="@+id/autocomplete_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:completionHintView="@layout/completion_hint_view"/>

第二个选项可能是最容易使用的,并且可以让您完全访问 TextView 的参数。如果您需要对此视图中的文本应用多种样式,您可以合并第一个建议,因为这将为您提供更大的灵活性。

如果这些建议都不够,我可以想到一些不太优雅的解决方法,可能会让您获得相同的结果。

Can you elaborate on what kind of styling you are seeking to apply?

If it's just basic text styling, you could probably build a Spannable and set the completion hint with the result, since it accepts a CharSequence. An example of building a Spannable and apply styles to it is illustrated in this post.

If you're looking for a way to actually manipulate the parameters of the TextView (e.g. padding), the source code of AutoCompleteTextView seems to provide a hint (pun intended).

private View getHintView(Context context) {
    if (mHintText != null && mHintText.length() > 0) {
        final TextView hintView = (TextView) LayoutInflater.from(context).inflate(
                mHintResource, null).findViewById(com.android.internal.R.id.text1);
        hintView.setText(mHintText);
        hintView.setId(HINT_VIEW_ID);
        return hintView;
    } else {
        return null;
    }
}

This reveals that Android looks for the id text1 in the resource reference you specify. The most basic version of such a resource would contain nothing but a TextView with this id:

<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:padding="10dp"
        android:textColor="#FF0000" android:textSize="16sp" />

Save above in a layout file (e.g. completion_hint_view.xml) and reference it as follows from your AutoCompleteTextView:

<AutoCompleteTextView android:id="@+id/autocomplete_textview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:completionHintView="@layout/completion_hint_view"/>

This second option is probably the easiest to use and will give you full access to the TextView's parameters. If you need to apply multiple styles to the text in this view, you can incorporate the first suggestion, as that will give you more flexibility.

If neither of these suggestions suffice, I can think of some less elegant work arounds that would probably allow you to get the same result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文