SharedPreference的用法

1.string.xml

<resources>

    <string name="app_name">sharedPreferences</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
	<string name="save">保存信息</string>
	<string name="load">读取信息</string>
</resources>

2.布局文件

activity_main

<LinearLayout 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:orientation="vertical" >
    
    <Button
        android:id="@+id/save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/save"
        />
    <Button
        android:id="@+id/load"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/load"
        />
   

</LinearLayout>

show_layout.xml

<LinearLayout 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:orientation="vertical" >
    
  	<TextView 
  	    android:id="@+id/show"
  	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
  	    android:text="@string/app_name"
  	    />
   

</LinearLayout>

3.Activity

SaveData

package com.example.sharedpreferences;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SaveData extends Activity{
	private static final String FILE_NAME="sharefile";
	private Button save=null;
	private Button load=null;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		save=(Button)findViewById(R.id.save);
		load=(Button)findViewById(R.id.load);
		save.setOnClickListener(new saveListener());
		load.setOnClickListener(new loadListener());
	}
	
	class saveListener implements OnClickListener{
		public void onClick(View v) {
			SharedPreferences share=getSharedPreferences(FILE_NAME, MODE_PRIVATE);//指定操作的文件名称
			SharedPreferences.Editor editor=share.edit();//编辑文件
			editor.putString("name", "zhangsan");
			editor.putString("phone", "13635603333");
			editor.commit();//提交更新
		}
	}
	
	class loadListener implements OnClickListener{
		public void onClick(View v) {
			Intent intent=new Intent(SaveData.this,ShowData.class);
			SharedPreferences preferences=getSharedPreferences(FILE_NAME, MODE_PRIVATE);//指定操作的文件名称
			String name=preferences.getString("name","");
			String phone=preferences.getString("phone","");
			intent.putExtra("name",name);
			intent.putExtra("phone",phone);
			startActivity(intent);
		}
	}
}

ShowData

package com.example.sharedpreferences;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class ShowData extends Activity {
	TextView textView=null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_layout);
        textView=(TextView) findViewById(R.id.show);
        Intent intent=getIntent();
        String name=intent.getStringExtra("name");
        String phone=intent.getStringExtra("phone");
        textView.setText(name+" "+phone);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值