[英]I don't understand Android AutoCompleteTextView Constructor of ArrayAdapter
我想在android中使用AutoCompleteTextView并阅读有关它的官方developer.android文档。
有一个代码片段,如下所示:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
我不明白ArrayAdapter的构造函数中的第二个参数(android.R.layout.simple_dropdown_item_1line)是什么意思,它来自哪里?
它是可从android获取的布局,还是我必须用自己创建的布局替换此布局,以及在这种情况下如何定义此布局文件?
具体我的代码象xml这样:
<AutoCompleteTextView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Java的:
AutoCompleteTextView search =(AutoCompleteTextView) findViewById(R.id.search);
String[] vocabs = new String[1001];
//fill the String array
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line ,vocabs);
search.setAdapter(adapter);
他们使用3个参数( 文档 )调用ArrayAdapter的构造函数: ArrayAdapter(Context context, int resource, T[] objects)
资源R.layout.simple_dropdown_item_1line只是下拉菜单的默认android框架布局之一。 请参阅此处其他默认布局的列表。
编辑以回答您的第二个问题 :
您可以使用默认的android布局(您提供的示例应该可以使用)或由您定义的自定义布局。 如果是最后一种情况,则只需为此布局创建一个xml布局:
布局/ dropdown_custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<TextView
android:id="@+id/vocab_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="vocab"/>
</LinearLayout>
然后,您可以使用ArrayAdapter构造函数ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
指向您的自定义布局以及要使用vocab填充的TextView:
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.dropdown_custom_view, R.id.vocab_text ,vocabs);
search.setAdapter(adapter);
还要检查您是否很好地填充了vocabs
数组。
这是Android系统中可用的布局,这是您使用android.R
的原因。它用于显示数组适配器中的项目。它基本上是带有某些样式的textview
您可以使用AutoCompleteTextView
的自定义布局,例如
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, R.layout.custom_layout, R.id.text_title, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.countries_list);
textView.setAdapter(adapter);
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e4e4e4"
>
<TextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
tools:text="AA"
android:padding="15dp"
/>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.