如何在Android的Autocomplete中显示搜索建议?

时间:2017-04-05 14:35:46

标签: android autocompletetextview autosuggest search-suggestion

作为用户,当我开始在搜索字段中输入内容时,我希望根据段落(字符串)中的匹配字词查看一些自动填充选项,这样我就不必输入真正的内容使用小巧的手机键盘长期使用。

注意:

建议将来自特定内容或字符串

2 个答案:

答案 0 :(得分:0)

以下链接适用于Android开发者论坛,可以帮助您入门。链接:https://developer.android.com/reference/android/widget/AutoCompleteTextView.html

用于创建文本视图的链接中提供的示例,用于在用户键入时建议各个国家/地区名称:

public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);

         //initialise the adapter for give n array of strings to be searched with
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         AutoCompleteTextView autotextView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);

         //set the adapter for the Autocomplete TextView
         autotextView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
 }

答案 1 :(得分:0)

试试这个,制作一个包含autoCompleteTextView的布局

<AutoCompleteTextView   
android:id="@+id/autoCompleteTextView1"     
android:layout_width="wrap_content"     
android:layout_height="wrap_content">

然后在java代码列表中列出所有建议,

String[] suggestions ={"Android","java"};

//or get it via array.xml of res, using 
//String[] suggestions = getResources().getStringArray(R.array.suggarray);

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

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

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