您的位置:首页 > 产品设计 > UI/UE

Android UI之AutoCompleteTextView

2016-01-14 07:12 531 查看
在应用开发过程中当为我们有如下需求在输入框中输入我们想要输入的信息,就会出现其想关的信息提示,这种效果在Android中可以使用AutoCompleteTextView 实现。

如下:

布局文件中添加组件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<AutoCompleteTextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:hint="输入想要查找的城市"

android:id="@+id/autoCompleteTextView"

android:layout_weight="1"

android:completionThreshold="2"

/>

</LinearLayout>

在目录values/

创建arrays.xml文件

<?xml version="1.0" encoding="utf-8"?>

<resources>

<array name="city_name">

<item>beijing</item>

<item>tianjiin</item>

<item>hebei</item>

<item>shandong</item>

<item>shanghai</item>

</array>

</resources>

在MainActivity中如下:

private AutoCompleteTextView autoCompleteTextView;

autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);

ArrayAdapter adapter = new ArrayAdapter.createFromResource(this, R.arry.city_name,android.R.Layout.simple_spinner_dropdown_item);

autoCompleteTextView.setAdapter(adapter);

在模拟器上运行如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: