使用SearchView按钮按钮搜索

时间:2017-03-19 03:55:56

标签: javascript android android-studio android-button searchview

我在content_main.xml

中实现了一些按钮
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.ivmenusisla.MainActivity"
        tools:showIn="@layout/app_bar_main">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/allRestaurants">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <Button
                    android:text="Blenders in the Grass"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/blenders"
                    android:alpha="0.90"
                    android:id="@+id/blenders" />

                <Button
                    android:text="Breakfast to Breakfast"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:drawableLeft="@drawable/btob"
                    android:alpha="0.90"
                    android:id="@+id/btob" />

            </LinearLayout>
        </ScrollView>

    </RelativeLayout>

我还在main.xml上添加了SearchView小部件

    <?xml version="1.0" encoding="utf-8"?>

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/action_search"
            android:icon="@android:drawable/ic_menu_search"
            android:title="search"
            app:actionViewClass="android.support.v7.widget.SearchView"
            app:showAsAction="always" />
    </menu>

如何设置动作侦听器,以便当用户单击SearchView小部件并键入内容时,会根据每个按钮的文本显示结果?

我的MainActivity.java上也有这个

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            //Log.e("onQueryTextChange", "called");
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {


            // Do your task here

            return false;
        }

    });

    return true;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

在Activity的“onCreate”功能中,你应该构建一个包含按钮'文本的列表。

List<String> textContent = new ArrayList();

... onCreate (...) {
   ...
   Button button1 = (Button) findViewById(R.id.blenders);
   Button button2 = (Button) findViewById(R.id.btob);

   textContent.clear();
   textContent.add(button1.getText());
   textContent.add(button2.getText());

   ...
}


    @Override
    public boolean onQueryTextSubmit(String query) {
        // Do your task here

        for(String text : textContent) {
           if(text.contain(query)) {
                // do what you wanna do
           }
        }

        return false;
    }