如何实现类似搜索栏的Google Play [英] How implement the Google Play like search bar

查看:126
本文介绍了如何实现类似搜索栏的Google Play的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用中实现搜索,它应该看起来像这样

I need to implement a search in my app and it should look like this

有人请足够小心向我解释一下它是如何完成的吗?预先感谢

would someone please care enough to explain to me how its done.?? Thanks in advance

推荐答案

您可以为此创建自定义布局. 1.采取相对布局,并为其提供圆角边框背景. 2.在其中添加汉堡图标. 3.在其中添加麦克风图标. 4.在中间添加edittext. 然后手动单击汉堡包"图标和麦克风"图标.

You can create a custom layout for that. 1. take a relative layout and give it a rounded bordered background to it. 2. add hamburger icon to left in it. 3. add mic icon to right in it. 4. Add edittext to it in the center. and then handle to click on both hamburger icon and mic icon manually.

我有一些代码不完全相同,但是您可以相应地进行编辑和更改.

I have some code which is not exactly same but you can edit and change it accordingly.

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:background="@drawable/editext_border">

            <ImageView
                android:id="@+id/iv_search_mic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_mic_green" />

            <EditText
                android:id="@+id/ed_home_searchbar"
                fontPath="fonts/weblysleekuisl.ttf"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_toLeftOf="@+id/iv_search_icon"
                android:layout_toRightOf="@+id/iv_search_mic"
                android:background="@android:color/transparent"
                android:hint="@string/action_search"
                android:imeOptions="actionSearch"
                android:padding="10dp"
                android:singleLine="true"
                android:textColor="@color/colorText"
                android:textCursorDrawable="@drawable/color_cursor"
                android:textSize="@dimen/text_xxsmall" />

            <ImageView
                android:id="@+id/iv_search_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_search_green" />

        </RelativeLayout>

然后处理点击事件,例如在麦克风点击上打开Goog​​le语音搜索

and then handle the click event like open google voice search on mic click

searchMic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OnSwap.getInstance().trackEvent(TAG, "searchMic", "searchMic Clicked");
                Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
                intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
                intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Search");
                startActivityForResult(intent, VOICE_INPUT_REQUEST_CODE);
            }
        });

然后是onActivityResult方法

and then onActivityResult method

if (requestCode == VOICE_INPUT_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                ArrayList<String> matches = data.getStringArrayListExtra(
                        RecognizerIntent.EXTRA_RESULTS);
                int matchSize = matches.size();
                for (int index = 0; index < matchSize; index++) {
                    Log.i(TAG, String.valueOf(index) + ": " + matches.get(index));
                    if (index == 0) {
                        searchbar.setText(matches.get(index));
                    }
                }
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(this, data);
                // TODO: Handle the error.
                Log.i(TAG, status.getStatusMessage());
                Snackbar.make(locationTextView, status.getStatusMessage(), Snackbar.LENGTH_LONG).show();

            } else if (resultCode == RESULT_CANCELED) {
                Snackbar.make(locationTextView, "Canceled", Snackbar.LENGTH_LONG).show();
            }
        }

这篇关于如何实现类似搜索栏的Google Play的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆