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<String> 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<String>(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<String>(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<hisArrays.length;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();
}


}



布局文件



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />


<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK">
</Button>


</LinearLayout>



更多相关文章

  1. mtk android settings
  2. Bitmap转为文件保存
  3. android apk的更新下载
  4. Android转盘抽奖的简单实现
  5. Android学习之使用HttpURLConnection下载文件
  6. android 文件的压缩和解压
  7. 83 Android(安卓)Handler ProgressBar 进度条
  8. Android文件操作放到listview中
  9. Android--一段兼容居中和满屏满屏的布局

随机推荐

  1. AndroidStudio 备忘录之Spinner(下拉列表)
  2. Android SDK 2.2 开发环境安装
  3. Android中的四种Activity
  4. APEX - Android(安卓)Q
  5. Android(安卓)Studio删除无用的资源文件
  6. Android的Zipalign优化
  7. Android图表控件MPAndroidChart——曲线
  8. Android中的CheckBox
  9. 【Android】 ListView之setEmptyView的问
  10. react native之修改APP的名称和图标