转载请注明出处:AutoCompleteTextView(单次)、MultiAutoCompleteTextView(多次)搜索自动补全、SearchView自动搜索_Mr_Leixiansheng的博客-CSDN博客
步骤:
AutoCompleteTextView
1、布局中加入AutoCompleteTextView控件,设置好参数(android:completionThreshold是设置从第几开始匹配,默认为2)
2、活动中
1)找到控件
2)设置适配器(同ListView)
3) 设置补全内容(适配器要用)
4)控件绑定适配器
MultiAutoCompleteTextView
前4步同上
5)设置 分隔符(区别)
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
代码如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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" tools:context="com.example.leixiansheng.autotext.MainActivity"> <!--默认第二个字符开始匹配。第一个字符开始匹配:android:completionThreshold="1"--> <AutoCompleteTextView android:id="@+id/auto_text" android:hint="请输入需要匹配内容·····" android:layout_width="match_parent" android:layout_height="wrap_content" /> <MultiAutoCompleteTextView android:id="@+id/mul_text" android:hint="多选择匹配" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/auto_text" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="37dp" /> </RelativeLayout>
package com.example.leixiansheng.autotext; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; /** * 自动补全 * 步骤: * 1、找到控件 * 2、配置适配器(同ListView) * 3、设置自动补全资源,如data * 4、控件绑定适配器 */ public class MainActivity extends AppCompatActivity { private AutoCompleteTextView autoCompleteTextView; private MultiAutoCompleteTextView multiAutoCompleteTextView; String[] datas = new String[]{"team", "tea", "text", "temp" ,"see","seak","set"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.auto_text); multiAutoCompleteTextView = (MultiAutoCompleteTextView) findViewById(R.id.mul_text); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas); autoCompleteTextView.setAdapter(adapter); multiAutoCompleteTextView.setAdapter(adapter); //区别,需要设置分隔符 multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }
SearchView自动搜索
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.SearchView android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.SearchView> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout>
package com.leixiansheng.test; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; /** * Created by Leixiansheng on 2017/9/12. */ public class SearchViewActivity extends AppCompatActivity { private static final String TAG = "SearchViewActivity"; private static final String[] contents = {"test", "text", "tea", "teach", "see", "sea", "set", "sell"}; private SearchView searchView; private ListView listView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_searchview); searchView = (SearchView) findViewById(R.id.search_view); listView = (ListView) findViewById(R.id.list_view); final ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contents); listView.setAdapter(arrayAdapter); listView.setTextFilterEnabled(true); searchView.setIconifiedByDefault(false); //直接显示搜索框,不隐藏 /** * 搜索监听 * FilterText 内容过滤器 */ searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { Log.i(TAG, newText); if (!TextUtils.isEmpty(newText)) { listView.setFilterText(newText); // arrayAdapter.getFilter().filter(newText); } else { listView.clearTextFilter(); } return true; } }); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(SearchViewActivity.this, listView.getAdapter().getItem(i).toString(), Toast.LENGTH_SHORT).show(); } }); } }
今天的文章
AlertDialog_深度搜索配合哪个软件分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/89809.html