android 搜索自动提示功能吗,Android 搜索框自动提示及其保存历史记录

package com.sun.com;

import android.app.Activity;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.AutoCompleteTextView;

import android.widget.Button;

import android.widget.Toast;

public class Count extends Activity {

private ArrayAdapter adapter_history;

private AutoCompleteTextView auto;

private Button ok;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.count);

auto = (AutoCompleteTextView) findViewById(R.id.auto);

auto.setThreshold(1);

ok = (Button) findViewById(R.id.save);

SharedPreferences sp = getSharedPreferences("history_strs", 0);

String save_history = sp.getString("history", "");

String[] hisArrays = save_history.split(",");

adapter_history = new ArrayAdapter(this,

android.R.layout.simple_dropdown_item_1line, hisArrays);

if (hisArrays.length > 5) {

String[] newArrays = new String[50];

System.arraycopy(hisArrays, 0, newArrays, 0, 50);

adapter_history = new ArrayAdapter(this,

android.R.layout.simple_dropdown_item_1line, newArrays);

}

auto.setAdapter(adapter_history);

ok.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Save();

}

});

}

private void Save() {

String text = auto.getText().toString();

SharedPreferences sp = getSharedPreferences("history_strs", 0);

String save_Str = sp.getString("history", "");

String[] hisArrays = save_Str.split(",");

for(int i=0;i

{

if(hisArrays[i].equals(text))

{

return;

}

}

StringBuilder sb = new StringBuilder(save_Str);

sb.append(text + ",");

sp.edit().putString("history", sb.toString()).commit();

Toast.makeText(Count.this, sb.toString(), Toast.LENGTH_LONG).show();

}

}

布局文件

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:id="@+id/auto"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:id="@+id/save"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="OK">

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值