今天心血来潮用as自动创建了一个登陆界面,一看xml布局,卧槽原来google用的是AutoCompleteTextView来当账号的控件的。马上查资料,应用场景就很多了,比如历史记录提示、搜索联想、文字自动完成(邮箱自动下拉一堆@qq.com,@163.com后缀)等等。
言归正传,文字自动完成需要实现写好字符集,搜索联想需要服务器配合获取数据,为了巩固记忆,今天实现把用户输入的作为历史记录保存,然后显示。
效果图:
![](https://img-blog.csdn.net/20170113170439792?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcTQxNzE5MDI0MA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
布局代码
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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.yang.app.autocompletetextview.MainActivity">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/auto_main_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint"
android:maxLines="1"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
<Button
android:background="#119999"
android:layout_gravity="center"
android:id="@+id/btn_main_save"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
代码
public class MainActivity extends AppCompatActivity {
private AutoCompleteTextView mAutoTv;
private Button mBtnSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initEvent();
}
private void initView() {
mAutoTv = (AutoCompleteTextView) findViewById(R.id.auto_main_text);
mBtnSave = (Button) findViewById(R.id.btn_main_save);
}
private void initData() {
initHistory();
}
private void initEvent() {
mAutoTv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
AutoCompleteTextView view = (AutoCompleteTextView) v;
if (hasFocus) {
SharedPreferences sp = getSharedPreferences("auto_text", MODE_PRIVATE);
String history = sp.getString("history", "");
if (!TextUtils.isEmpty(history))
view.showDropDown();
}
}
});
mBtnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveHistory(mAutoTv);
}
});
}
/**
* 初始化历史记录,最多显示3项提示,采用sp保存
*/
int nearHistory = 3;
private void initHistory() {
SharedPreferences sp = getSharedPreferences("auto_text", MODE_PRIVATE);
String history = sp.getString("history", "");
String[] hisArrays = history.split(",");
ArrayAdapter<String> hisAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, hisArrays);
if (hisArrays.length > nearHistory) {
String[] newArrays = new String[nearHistory];
System.arraycopy(hisArrays, 0, newArrays, 0, nearHistory);
hisAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, newArrays);
}
mAutoTv.setAdapter(hisAdapter);
mAutoTv.setThreshold(1);
mAutoTv.setCompletionHint("最近的3条记录");
}
/**
* 把内容保存到sp中
*/
private void saveHistory(AutoCompleteTextView autoTv) {
String text = autoTv.getText().toString();
SharedPreferences sp = getSharedPreferences("auto_text", MODE_PRIVATE);
String history = sp.getString("history", "");
if (!TextUtils.isEmpty(text)&&!history.contains(text + ",")) {
StringBuilder sb = new StringBuilder(history);
sb.insert(0, text + ",");
sp.edit().putString("history", sb.toString()).apply();
Toast.makeText(MainActivity.this, "存好了", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "存过了", Toast.LENGTH_SHORT).show();
}
}
}
是不是很简单,并没有多少需要写的。但是特别注意的是:控件的setAdapter()方法,参数必须是继承ListAdapter和Filterable的,其中arrayAdapter和simpleAdapter都能满足要求,推荐用arrayAdapter,simpleAdapte还要设置他的显示位置和textview组件。
另外还查到了MultiAutoCompleteTextView,一个可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配。这个可用在发短信,发邮件时选择联系人这种类型当中。使用时需要执行设置分隔符方法。
Demo:AutoCompleteTextViewDemo