📜  带有示例的Android SearchView

📅  最后修改于: 2021-05-09 17:06:36             🧑  作者: Mango

搜索视图窗口小部件用于为用户提供搜索界面,以便用户可以输入其搜索查询并向搜索提供程序提交请求,并获取查询建议或结果列表。

类语法:

public class SearchView
extends LinearLayout
implements CollapsibleActionView

类层次结构:

java.lang.Object
  ↳  android.view.View
    ↳  android.view.ViewGroup
      ↳  android.widget.LinearLayout
        ↳  android.widget.SearchView

演示SearchView的示例:
在本文中,您将创建具有搜索视图和列表视图的基本搜索应用程序。用户将在操作栏中显示的搜索视图中键入搜索查询。以下是说明步骤:

  • 步骤1:activity_main.xml

    它由带有ListView的相对布局组成,从中可以搜索数据。这是activity_main.xml的完整代码:

    activity_main.xml
    
    
      
    
      
        
        
      
    


    menu.xml
      
      
       
      
        
          
    


    MainActivity.java
    package com.geeksforgeeks.searchviewwidget;
      
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.widget.ArrayAdapter;
    import android.widget.SearchView;
    import java.util.ArrayList;
      
    public class MainActivity extends AppCompatActivity {
      
        // List View object
        ListView listView;
      
        // Define array adapter for ListView
        ArrayAdapter adapter;
      
        // Define array List for List View data
        ArrayList mylist;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            // initialise ListView with id
            listView = findViewById(R.id.listView);
      
            // Add items to Array List
            mylist = new ArrayList<>();
            mylist.add("C");
            mylist.add("C++");
            mylist.add("C#");
            mylist.add("Java");
            mylist.add("Advanced java");
            mylist.add("Interview prep with c++");
            mylist.add("Interview prep with java");
            mylist.add("data structures with c");
            mylist.add("data structures with java");
      
            // Set adapter to ListView
            adapter
                = new ArrayAdapter(
                    this,
                    android.R.layout.simple_list_item_1,
                    mylist);
            listView.setAdapter(adapter);
        }
      
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            // Inflate menu with items using MenuInflator
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu, menu);
      
            // Initialise menu item search bar
            // with id and take its object
            MenuItem searchViewItem
                = menu.findItem(R.id.search_bar);
            SearchView searchView
                = MenuItemCompat
                      .getActionView(searchViewItem);
      
            // attach setOnQueryTextListener
            // to search view defined above
            searchView.setOnQueryTextListener(
                new SearchView.OnQueryTextListener() {
      
                    // Override onQueryTextSubmit method
                    // which is call
                    // when submitquery is searched
      
                    @Override
                    public boolean onQueryTextSubmit(String query)
                    {
                        // If the list contains the search query
                        // than filter the adapter
                        // using the filter method
                        // with the query as its argument
                        if (list.contains(query)) {
                            adapter.getFilter().filter(query);
                        }
                        else {
                            // Search query not found in List View
                            Toast
                                .makeText(MainActivity.this,
                                          "Not found",
                                          Toast.LENGTH_LONG)
                                .show();
                        }
                        return false;
                    }
      
                    // This method is overridden to filter
                    // the adapter according to a search query
                    // when the user is typing search
                    @Override
                    public boolean onQueryTextChange(String newText)
                    {
                        adapter.getFilter().filter(newText);
                        return false;
                    }
                });
      
            return super.onCreateOptionsMenu(menu);
        }
    }


  • 步骤2:menu.xml

    创建menu.xml并将搜索视图作为菜单项添加到操作栏,标题为“搜索”此文件。这是menu.xml文件的完整代码。

    menu.xml

      
      
       
      
        
          
      
    
  • 步骤3:MainActivity。Java
    下一步是设置主要活动的代码。在此Java文件中,将项目手动添加到列表视图,并将setOnQueryTextListener附加到搜索视图。 onQueryTextSubmit()方法被覆盖,其中List View根据用户输入的搜索查询获取过滤器。这里是MainActivity的完整代码。 Java的:

    主要活动。Java

    package com.geeksforgeeks.searchviewwidget;
      
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.widget.ArrayAdapter;
    import android.widget.SearchView;
    import java.util.ArrayList;
      
    public class MainActivity extends AppCompatActivity {
      
        // List View object
        ListView listView;
      
        // Define array adapter for ListView
        ArrayAdapter adapter;
      
        // Define array List for List View data
        ArrayList mylist;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            // initialise ListView with id
            listView = findViewById(R.id.listView);
      
            // Add items to Array List
            mylist = new ArrayList<>();
            mylist.add("C");
            mylist.add("C++");
            mylist.add("C#");
            mylist.add("Java");
            mylist.add("Advanced java");
            mylist.add("Interview prep with c++");
            mylist.add("Interview prep with java");
            mylist.add("data structures with c");
            mylist.add("data structures with java");
      
            // Set adapter to ListView
            adapter
                = new ArrayAdapter(
                    this,
                    android.R.layout.simple_list_item_1,
                    mylist);
            listView.setAdapter(adapter);
        }
      
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            // Inflate menu with items using MenuInflator
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu, menu);
      
            // Initialise menu item search bar
            // with id and take its object
            MenuItem searchViewItem
                = menu.findItem(R.id.search_bar);
            SearchView searchView
                = MenuItemCompat
                      .getActionView(searchViewItem);
      
            // attach setOnQueryTextListener
            // to search view defined above
            searchView.setOnQueryTextListener(
                new SearchView.OnQueryTextListener() {
      
                    // Override onQueryTextSubmit method
                    // which is call
                    // when submitquery is searched
      
                    @Override
                    public boolean onQueryTextSubmit(String query)
                    {
                        // If the list contains the search query
                        // than filter the adapter
                        // using the filter method
                        // with the query as its argument
                        if (list.contains(query)) {
                            adapter.getFilter().filter(query);
                        }
                        else {
                            // Search query not found in List View
                            Toast
                                .makeText(MainActivity.this,
                                          "Not found",
                                          Toast.LENGTH_LONG)
                                .show();
                        }
                        return false;
                    }
      
                    // This method is overridden to filter
                    // the adapter according to a search query
                    // when the user is typing search
                    @Override
                    public boolean onQueryTextChange(String newText)
                    {
                        adapter.getFilter().filter(newText);
                        return false;
                    }
                });
      
            return super.onCreateOptionsMenu(menu);
        }
    }
    

输出:

  • 带有搜索栏和列表的主要活动
  • 根据搜索查询的搜索结果
  • 搜索结果