关于AutoCompleteTextView实现前几个历史提示

代码不多,有关方法参数的请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();
        }
    }
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值