android学习笔记26--------------AutoCompleteTextView 与 MultiAutoCompleteTextView
1、简介
在我们平常上网的时候经常会用到谷歌或百度,在输入框中输入我们想要输入的信息就会出现其他与其相关的提示信息,非常方便。这种效果在Android中是用AutoCompleteTextView实现的AutoCompleteTextView是一个可以编辑的文本view,当用户键入时,会自动显示完成建议信息。
建议列表显示在下拉列表框中,可以选中某项代替编辑框里的内容。
当用户点击回车键时,或者什么也没有选中点击ENTER建时下拉列表会自动消失。
建议列表是从一个数据适配器获取的数据。
[java]
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.AdapterView.OnItemClickListener;
public class AutoActivity extends Activity
{
private AutoCompleteTextView autoCompleteTextView = null;
private final String[] Citys = new String[]
{
"Belgium", "France", "Italy", "Germany", "Spain", "Sppp", "Itaggg"
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.AutoCompleteTextView);
// 配置设配器
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
AutoActivity.this,
android.R.layout.易做图_dropdown_item_1line, Citys);
autoCompleteTextView.setAdapter(arrayAdapter);
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
System.out.println(autoCompleteTextView.getText().toString());
}
});
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.AdapterView.OnItemClickListener;
public class AutoActivity extends Activity
{
private AutoCompleteTextView autoCompleteTextView = null;
private final String[] Citys = new String[]
{
"Belgium", "France", "Italy", "Germany", "Spain", "Sppp", "Itaggg"
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.AutoCompleteTextView);
// 配置设配器
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
AutoActivity.this,
android.R.layout.易做图_dropdown_item_1line, Citys);
autoCompleteTextView.setAdapter(arrayAdapter);
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
System.out.println(autoCompleteTextView.getText().toString());
}
});
}
}
[java]
<span style="white-space: pre; background-color: rgb(240, 240, 240); "><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span>
<span style="white-space: pre; background-color: rgb(240, 240, 240); "><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span> [java]
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<AutoCompleteTextView
android:id="@+id/AutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
/>
</LinearLayout>
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<AutoCompleteTextView
android:id="@+id/AutoCompleteTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
/>
</LinearLayout>
补充:
completionThreshold:它的值决定了你在AutoCompleteTextView至少输入几个字符,它才会具有自动提示的功能。另,默认最多提示20条。
dropDownAnchor:它的值是一个View的ID,指定后,AutoCompleteTextView会在这个View下弹出自动提示。
dropDownSelector:应该是设置自动提示的背景色之类的,没有尝试过,有
补充:移动开发 , Android ,