AutoCompleteTextView 就是输入账号密码可以自动补全的那个功能,android 给你封装好了,
网上看教程之后
就是setAdapter 不进去
auto.setAdapter(adapter);
Log.d(TAG, "setAdapter");
auto.setDropDownHeight(1500);
auto.setThreshold(1);
auto.setCompletionHint("最近的5条记录");
auto.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(hasWindowFocus()){
((AutoCompleteTextView) view).showDropDown();
Log.d(TAG, "onFocusChange: showdown");
}
}
});
里面有一个 setDropDownHeight(1500),
一开始想想70dp 总够大了吧。。
所以下来的框太窄显示不出什么的。。。
另外存储使用了sharedPerference。
下面贴源代码:
xml
java
package com.example.expriment12;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;
public class ThirdActivity extends AppCompatActivity {
private Button conform;
private AutoCompleteTextView auto1,auto2;
private ArrayAdapter adapter;
private SharedPreferences.Editor editer;
private String TAG = "weimeng";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
conform = (Button) findViewById(R.id.conform);
auto1 = (AutoCompleteTextView) findViewById(R.id.auto1);
auto2 = (AutoCompleteTextView) findViewById(R.id.auto2);
editer = getSharedPreferences("data",MODE_PRIVATE).edit();
initAutoComplete("history1",auto1);
initAutoComplete("history2",auto2);
conform.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveHistory("history1",auto1);
saveHistory("history2",auto2);
Toast.makeText(ThirdActivity.this, "保存了上述账号和密码已经完成了任务,不用求登陆", Toast.LENGTH_SHORT).show();
}
});
}
private void initAutoComplete(String field,AutoCompleteTextView auto) {
SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
String longHistory = pref.getString(field, "nothing");
Log.d(TAG, "initAutoComplete: +longHistory"+longHistory);
String[] hisArrays = longHistory.split(",");
Log.d(TAG, "hisArrays length="+hisArrays.length);
adapter = new ArrayAdapter(this,
android.R.layout.simple_dropdown_item_1line, hisArrays);
//只保留最近的50条记录
if (hisArrays.length > 50) {
String[] newArray = new String[50];
System.arraycopy(hisArrays, 0, newArray, 0, 50);
adapter = new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,
newArray);
}
auto.setAdapter(adapter);
Log.d(TAG, "setAdapter");
auto.setDropDownHeight(1500);
auto.setThreshold(1);
auto.setCompletionHint("最近的5条记录");
auto.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(hasWindowFocus()){
((AutoCompleteTextView) view).showDropDown();
Log.d(TAG, "onFocusChange: showdown");
}
}
});
}
//放入 pref 的 field 字段中
private void saveHistory(String field,AutoCompleteTextView auto) {
String text = auto.getText().toString();
SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
String longHistory = sp.getString(field, "nothing");
Log.d(TAG, "saveHistory: "+longHistory);
//避免重复
if (!longHistory.contains(text + ",")) {
StringBuilder sb = new StringBuilder(longHistory);
sb.insert(0, text + ",");
editer.putString(field, sb.toString()).apply();
}
}
}
效果图:
以为一点别的地方就没了,所以没法手机截屏。。
电脑前置摄像头拍了一下:是镜像。。。模糊了点不过效果是有的