使用SharePreferences保存key-value对步骤:
第一步:使用Activity.getSharePreferences方法获得SharedPreferences对象。
第二步:使用SharedPreferences获取SharedPreferences.Editor对象。
第三步:使用SharedPreferences.Editor.putxxx方法保存key-value对。
第四步:通过SharedPreferences.Editor.commit方法保存key-value对。
例子:
程序清单1
MainActivity.java
package xiaocool.net.memory;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button bt1;
private Button bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=(Button)findViewById(R.id.but);
bt2=(Button)findViewById(R.id.but2);
bt1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Context ctx=MainActivity.this;
SharedPreferences sp=ctx.getSharedPreferences("SP",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("name","李宁");
editor.putString("age", "20");
// editor.putBoolean("BOOLEAN_KEY", true);
editor.commit();
// Log.d("SP", sp.getString("STRING_KEY", "none"));
// Log.d("SP", sp.getString("NOT_EXIST", "none"));
}
});
bt2.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Context ctx=MainActivity.this;
SharedPreferences sp=ctx.getSharedPreferences("SP",MODE_PRIVATE);
String name=sp.getString("name","");
String age=sp.getString("age","");
Toast.makeText(MainActivity.this,"name:"+name+"\n"+"age:"+age,Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
程序清单2
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写数据"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="104dp" />
<Button
android:id="@+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读数据"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
效果图: