android开发———数据存储之SharedPreferences

使用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>


效果图:




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值