Android 可点击 TextView,带有提示和图像,用于启动搜索对话框并显示所选结果

发布于 2024-10-24 11:56:48 字数 177 浏览 5 评论 0原文

我正在寻找一个示例,该示例展示了如何实现可单击的文本视图,该文本视图启动 Android 默认搜索对话框并显示选定的结果行。

它应该与 Android 上的 Google 地图操作栏中的搜索字段具有相同的行为和设计(例如,左侧的放大镜玻璃图标、如果文本视图为空则显示“搜索”提示、单击可启动由可搜索定义的搜索对话框)入口):

I'm searching for an example that shows how I can implement a clickable textview that starts the Android default search dialog and displays a selected result line.

It should have the same behaviour and design as the search field in the Google Maps action bar on Android (e. g. magnifier glass icon on the left, a "Search" hint if the textview is empty, a click starts the search dialog defined by a searchable entry):

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

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

发布评论

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

评论(3

潦草背影 2024-10-31 11:56:48

结果的自定义 EditText

public class SearchEditText extends EditText {

    private boolean mMagnifyingGlassShown = true;
    private Drawable mMagnifyingGlass;

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMagnifyingGlass = getCompoundDrawables()[0];
    }

    /**
     * Conditionally shows a magnifying glass icon on the left side of the text field
     * when the text it empty.
     */
    @Override
    public boolean onPreDraw() {
        boolean emptyText = TextUtils.isEmpty(getText());
        if (mMagnifyingGlassShown != emptyText) {
            mMagnifyingGlassShown = emptyText;
            if (mMagnifyingGlassShown) {
                setCompoundDrawables(mMagnifyingGlass, null, null, null);
            } else {
                setCompoundDrawables(null, null, null, null);
            }
            return false;
        }
        return super.onPreDraw();
    }

这是带有复合可绘制对象和 xml

<view
                class="com.tr.search.SearchEditText"
                android:id="@+id/search_src_text"
                android:layout_height="wrap_content"
                android:layout_width="0dip"
                android:layout_weight="1.0"
                android:singleLine="true"
                android:ellipsize="end"
                android:hint="@string/search_bar_hint"
                android:drawableLeft="@drawable/magnifying_glass"
                android:freezesText="true"
            />

在此处输入图像描述

That is custom EditText with compound drawable

public class SearchEditText extends EditText {

    private boolean mMagnifyingGlassShown = true;
    private Drawable mMagnifyingGlass;

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mMagnifyingGlass = getCompoundDrawables()[0];
    }

    /**
     * Conditionally shows a magnifying glass icon on the left side of the text field
     * when the text it empty.
     */
    @Override
    public boolean onPreDraw() {
        boolean emptyText = TextUtils.isEmpty(getText());
        if (mMagnifyingGlassShown != emptyText) {
            mMagnifyingGlassShown = emptyText;
            if (mMagnifyingGlassShown) {
                setCompoundDrawables(mMagnifyingGlass, null, null, null);
            } else {
                setCompoundDrawables(null, null, null, null);
            }
            return false;
        }
        return super.onPreDraw();
    }

And xml

<view
                class="com.tr.search.SearchEditText"
                android:id="@+id/search_src_text"
                android:layout_height="wrap_content"
                android:layout_width="0dip"
                android:layout_weight="1.0"
                android:singleLine="true"
                android:ellipsize="end"
                android:hint="@string/search_bar_hint"
                android:drawableLeft="@drawable/magnifying_glass"
                android:freezesText="true"
            />

Result enter image description here

む无字情书 2024-10-31 11:56:48

首先,您必须使用以下代码使您的 textView 可点击

TextView txtView =findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener(View v){
new OnClickListener() {

            @Override
        public void onClick(View v) {
            //Your Code for calling search ativity
        }
    });

以进行搜索活动,请参阅 这里

还给出了android sdk中搜索对话框的一个示例
如果您使用的是 eclipse,那么只需单击

File->new->android Project->select sdk
->create project from  existing project sample
select searchableDictionary

well first you have to make your textView clickable with following code

TextView txtView =findViewById(R.id.txtView);
txtView.setOnClickListener(new OnClickListener(View v){
new OnClickListener() {

            @Override
        public void onClick(View v) {
            //Your Code for calling search ativity
        }
    });

for search activity plz refer here

there is also one example given for search dialog in android sdk
if you are using eclipse, then just click on

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