android怎么保存字符串数组中,Android:如何在Android的SharedPreferences中存储字符串数组...

我正在构建一个使用搜索引擎搜索网络的应用程序.我的应用程序中有一个edittext,用户将从中搜索网页.我想像浏览器历史一样保存搜索关键字.我可以使用最后一个关键字保存并显示它,但我不能增加搜索结果的数量.我想显示最近的5个searhes.这是我的代码:

public class MainActivity extends Activity implements OnClickListener {

Button insert;

EditText edt;

TextView txt1, txt2, txt3, txt4, txt5;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

edt = (EditText) findViewById(R.id.search_word);

txt1 = (TextView) findViewById(R.id.txt1);

txt1.setOnClickListener(this);

insert = (Button) findViewById(R.id.insert);

insert.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

if ((edt.getText().toString().equals(""))) {

Toast.makeText(

getBaseContext(),

"Whoa! You haven't entered anything in the search box.",

Toast.LENGTH_SHORT).show();

} else {

SharedPreferences app_preferences = PreferenceManager

.getDefaultSharedPreferences(MainActivity.this);

SharedPreferences.Editor editor = app_preferences.edit();

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

editor.putString("key", text);

editor.commit();

Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG)

.show();

}

}

});

}

@Override

protected void onStart() {

// TODO Auto-generated method stub

super.onStart();

SharedPreferences app_preferences = PreferenceManager

.getDefaultSharedPreferences(this);

String text = app_preferences.getString("key", "null");

txt1.setText(text);

}

public void onClick(View v) {

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

Toast.makeText(getBaseContext(), text, Toast.LENGTH_SHORT).show();

}

}

请帮我克服这个问题.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值