如何使用自定义搜索视图进行自动搜索

时间:2017-01-05 11:40:02

标签: android listview autocomplete searchview

我想让我的应用程序成为自动搜索。

这是我的应用程序。

我的应用

enter image description here

我希望我的应用能够像这样制作

我想这样做:

enter image description here

但是怎么样?任何人都可以帮助我吗?



这是我的代码



MainActivity.class

package com.skholingua.android.searchview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;

public class MainActivity extends Activity implements
        SearchView.OnQueryTextListener {
    String[] stateList;
    String[] anotherStringArray; 
    private SearchView searchView;
    private ListView listView;
    private ArrayAdapter<String> adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        searchView = (SearchView) findViewById(R.id.searchView1);
        listView = (ListView) findViewById(R.id.listView1);
        stateList = getResources().getStringArray(R.array.stateList);
        anotherStringArray = getResources().getStringArray(R.array.anotherStringArray);

        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, stateList);
        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);


        // Sets the default or resting state of the search field.
        // If true, a single search icon is shown by default and
        // expands to show the text field and other buttons when pressed
        //searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(this);

        // Sets the hint text to display in the query text field
        //searchView.setQueryHint("State Name");

        int searchPlateId = searchView.getContext().getResources()
                .getIdentifier("android:id/search_plate", null, null);
        View searchPlateView = searchView.findViewById(searchPlateId);
        if (searchPlateView != null) {
            searchPlateView.setBackgroundColor(Color.TRANSPARENT);
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String stateName = listView.getAdapter().getItem(position).toString();
                int pos=0;
                for(int i=0;i<stateList.length;i++)
                {
                    if(stateList[i].equals(stateName))
                    {
                        pos=i;
                        break;
                    }
                }
                // Put selected state to intent.
                Intent intent = new Intent(MainActivity.this, NextActivity.class);
                intent.putExtra("selectedState", stateName);
                intent.putExtra("anotherStringArray", anotherStringArray[pos]);
                startActivity(intent);
            }
        });

    }



    @Override
        public boolean onQueryTextChange(String newText) {
            android.widget.Filter filter = adapter.getFilter();
            if (TextUtils.isEmpty(newText)) {
                filter.filter("");
            } else {
                filter.filter(newText);
            }
            return true;
        }



    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @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);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
}   
}

activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:focusableInTouchMode="true"
    tools:context="com.skholingua.android.searchview.MainActivity" >

    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@drawable/search_view_border"
        android:iconifiedByDefault="false"
        android:padding="2dp"
        android:queryHint="Search...."
        android:layout_alignParentTop="true" >

    </SearchView>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/searchView1"
        android:layout_centerHorizontal="true" >

    </ListView>

</RelativeLayout>

3 个答案:

答案 0 :(得分:1)

也许使用AutoCompleteTextView可以解决您的问题?

Google Documentation

答案 1 :(得分:0)

答案 2 :(得分:0)

试试此代码

public class MainActivity extends Activity {
AutoCompleteTextView text;
String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);

  ArrayAdapter adapter = new 
     ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);

  text.setAdapter(adapter);
  text.setThreshold(1);

} }

并使用'AutoCompleteTextView'来放置文本视图或编辑文本 有关更多帮助,请参阅下面提到的链接 https://www.tutorialspoint.com/android/android_auto_complete.htm