代码不多,有关方法参数的请everything下,没有耐心看的请右上离开!!!!!!!!!!!!
final AutoCompleteTextView user_name = (AutoCompleteTextView) findViewById(R.id.user_name);
//获取内容
SharedPreferences sp = getSharedPreferences("search_history", 0);
String history = sp.getString("history", "");
String[] history_arr = history.split(",");
user_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, history_arr);
//取最前三个
if (history_arr.length > 3) {
String[] temp_use = new String[3];
System.arraycopy(history_arr, history_arr.length - 3, temp_use, 0, 3);
user_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, temp_use);
}
user_name.setAdapter(user_name_adapter);
Button enter = (Button) findViewById(R.id.enter);
Button sign_in = (Button) findViewById(R.id.sign_in);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!user_name.getText().toString().equals("")) {
SharedPreferences mysp = getSharedPreferences("search_history", 0);
String text_name = user_name.getText().toString();
String old_text = mysp.getString("history", "");
StringBuilder builder = new StringBuilder(old_text);
builder.append(text_name + ",");
//判断是否存在有重复数据
if (!old_text.contains(text_name + ",")) {
SharedPreferences.Editor myEditor = mysp.edit();
myEditor.putString("history", builder.toString());
myEditor.commit();
Toast.makeText(getApplication(), text_name + "添加成功", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplication(), text_name + "已经存在", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplication(), "请输入用户名", Toast.LENGTH_LONG).show();
}
}
});