AutoCompleteTextView控件使用一个adapter类包装数据源,使得可以根据我们的输入信息,从包装的数据源中找到匹配的列表项给我们提示,我们只要选择我们需要的数据项就可以完成输入操作。所以也就顺应了这个控件的名字是自动完成的文本框,
实现这个的目的就要分下面三步:
- 在布局文件中实现AutoCompleteTextView控件
- 在对于的activity中实现一个adapter作为AutoCompleteTextView控件的提示信息来源,adapter绑定了一个提示字符串数组
- 将上面的adapter和AutoCompleteTextView绑定
下面以上面三点来叙述实现步奏
1
<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"
android:background="#F11"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.uitest.SencondActivity" >
<TextView
android:id="@+id/tvshow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="代表这个页面表示的信息"
tools:ignore="HardcodedText" />
<Button
android:id="@+id/invokeActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="invoke"
android:text="启动新的页面"
tools:ignore="HardcodedText" />
<!-- 很简单吧 -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:ignore="LabelFor" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:text="Please input the text:"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<EditText
android:id="@+id/ET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="只能输入10位数字"
android:inputType="number"
tools:ignore="HardcodedText" />
</LinearLayout>
</LinearLayout>
2
在java代码中创建一个adapter,并绑定到AutoCompleteTextView控件
package com.example.uitest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class SencondActivity extends Activity {
private final static String TAG_SENCOND_ACTIVITY_STRING = "SencondActivity";
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
AutoCompleteTextView actView;
TextView tView;
EditText mEditText;
//加入提示信息的方法,这里使用simpleAdapter这个adapter
public void addItems() {
Map<String, String> itemMap;
// 第一个map
itemMap = new HashMap<String, String>();
itemMap.put("brandSearchText", "小米为发烧而生");
itemMap.put("brandName", "小米");
list.add(itemMap);
// 第二个map
itemMap = new HashMap<String, String>();
itemMap.put("brandSearchText", "乐视,年轻人的第一台电视");
itemMap.put("brandName", "乐视");
list.add(itemMap);
// 第三个map
itemMap = new HashMap<String, String>();
itemMap.put("brandSearchText", "三星来自大韩明国手机业者的佼佼者");
itemMap.put("brandName", "三星");
list.add(itemMap);
// 第四个map
itemMap = new HashMap<String, String>();
itemMap.put("brandSearchText", "苹果引领全球科技潮流");
itemMap.put("brandName", "苹果");
list.add(itemMap);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sencond);
tView = (TextView) findViewById(R.id.tvshow);
tView.setText("Activity为" + this.toString() + "\n TaskID为:"
+ this.getTaskId());
addItems();// 给list增加元素
actView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
//构造一个adapter
SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.item_row,
new String[] { "brandSearchText", "brandName" }, new int[] {
R.id.searchText, R.id.brandName });
// 给自动完成文本框绑定adapter
actView.setAdapter(notes);
actView.setThreshold(1);//设定当输入一个字母时候激起提示
actView.setOnItemClickListener(new OnItemClickListener() {
//这个方法主要功能是,当我们单击一个提示项时候,响应这个方法,我们可以
//根据单击的这个列表项的内容组织给这个自动完成文本框的文本。
//而不是{brandName=三星,brandSearchText=三星来自大韩明国手机业者的佼佼者}
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
//这里要注意这里findviewById方法前面的view。这个很重要不要忘记了。忘记了很麻烦
//空指针找你麻烦
TextView tView = (TextView) view.findViewById(R.id.brandName);
TextView tView2 = (TextView) view.findViewById(R.id.searchText);
actView.setText(tView.getText().toString() + ", "
+ tView2.getText().toString());
// 将鼠标光标移动到actView.getText().toString()这个字符串的末尾位置
actView.setSelection((actView.getText().toString()).length());
Log.i(TAG_SENCOND_ACTIVITY_STRING, ((TextView) view
.findViewById(R.id.brandName)).getText().toString());
Log.i(TAG_SENCOND_ACTIVITY_STRING, ((TextView) view
.findViewById(R.id.searchText)).getText().toString());
}
});
mEditText = (EditText) findViewById(R.id.ET);
mEditText.addTextChangedListener(mTextWatcher);
}
TextWatcher mTextWatcher = new TextWatcher() {
private CharSequence temp;
private int editStart;
private int editEnd;
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
temp = s;
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
editStart = mEditText.getSelectionStart();
editEnd = mEditText.getSelectionEnd();
if (temp.length() > 10) {
Toast.makeText(SencondActivity.this, "你干那", Toast.LENGTH_LONG)
.show();
s.delete(editStart - 1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
}
};
}
3
adapter每项提示项布局,很简单就是两个竖排的textview
<?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/brandName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="123" />
<TextView
android:id="@+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="456"/>
</LinearLayout>
参考链接
pengruikeji
iamkila