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);
在模拟器上运行如下:
如下:
布局文件中添加组件
<?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);
在模拟器上运行如下:
相关文章推荐
- AndroidUI基础之Spinner的使用
- Divide and conquer:Aggressive Cows(POJ 2456)
- UIDynamic—UIKit 动力学
- 【UE4学习】03——Blueprint快速入门
- Jfinal 2.1 集成 Guice,实现注解功能, 加入Service接口和实现类,直接晒代码
- UIView常用属性的注意点:
- 【iOS开发】值对象--NSValue,NSNumber
- 解决相片上传主键唯一性问题,java 上传相片到服务器的时候,相片保存主键用guid。
- request response
- UIImagePickerController详解
- 关于NGUI动态加载图片,并且实现动态打图集的过程
- serialVersionUID的作用
- 关于NGUI动态加载图片,并且实现动态打图集的过程
- - (id)valueForKeyPath:(NSString *)keyPath 用法总结
- [UIImage resizableImageWithCapInsets:] 使用方法
- Java集合:队列: Queue
- IBM Bluemix
- 设计模式之Builder模式
- WebKit之Chromium的UI绘制流程初探
- WPF:Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension'