1

作为用户,当我开始在搜索字段中输入内容时,我希望根据段落(字符串)中的匹配项来查看一些自动完成选项,这样我就不必使用小巧的电话键盘输入真正的长期条款。如何在Android自动完成中显示搜索建议?

注:

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

+0

你使用了什么适配器? – pskink

+1

试试这个教程https://www.tutorialspoint.com/android/android_auto_complete.htm –

+0

在适配器中发布你的过滤器代码,或者如果你使用库 – Nithinlal

回答

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" 
    }; 
} 
+0

你注意到OP使用' autocompletetextview'标记? – pskink

+0

@kaushik他已经提到 – Nithinlal

+0

哦,我的坏。让我改变这一点。 @pskink –

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);