maomaoloverr

安卓,自动填充文本框

2015-10-09 18:38  潘猫猫  阅读(1193)  评论(0编辑  收藏  举报

  AutoCompleteTextView从EditText派生出来,实际上也是一个文本编辑框,但它比普通编辑框多一个功能:

当用户输入一个字符后,自动完成文本框会显示一个下拉菜单,供用户从中选择,当用户选择某个菜单项之后,AutoCompleteTextView按用户选择自动填写该文本框。

 

使用AutoCompleteTextView要为它设置一个Adapter适配器,该Adapter封装了AutoCompleteTextView预设的提示文本。在常见的View(ListView,GridView)等地方都需要用到Adapter。

 

 1 package com.example.demo2;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 import android.widget.ArrayAdapter;
 7 import android.widget.AutoCompleteTextView;
 8 
 9 public class MainActivity extends Activity {
10     //定义字符串数组,作为提示的文本
11     String[] books = new String[]{
12             "aaaa",
13             "aaabbb",
14             "aaabbbbccc",
15             "aaabbbcccddd",
16             "abc",
17             "abcdfe",
18             "aavaa",
19             "aaacbbb",
20             "aaaxbbbbccc",
21             "aaabbbccccddd",
22             "abxc",
23             "abxcdfe"
24     };
25     private AutoCompleteTextView auto;
26     @Override
27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.activity_main);
30         //
31         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, 
32                 android.R.layout.simple_dropdown_item_1line, books);
33         //
34         auto = (AutoCompleteTextView) findViewById(R.id.auto);
35         //
36         auto.setAdapter(arrayAdapter);
37     }
38 
39     
40 }

xml文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7   <!-- 定义一个自动完成文本框,指定输入一个字符后进行提示 -->
 8   <AutoCompleteTextView 
 9       android:id="@+id/auto"
10       android:layout_width="fill_parent"
11       android:layout_height="wrap_content"
12       android:dropDownHorizontalOffset="20dp"
13       android:completionThreshold="1"
14   />
15   <!-- 设置下拉菜单与文本框之间的水平偏移 :dropDownHorizontalOffset
16           指明当输入多少个字的时候给出响应的提示:completionThreshold-->
17           
18 </LinearLayout>

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-08-11
  • 2022-01-28
  • 2021-07-29
  • 2021-11-03
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-14
  • 2022-12-23
  • 2021-05-12
相关资源
相似解决方案