Android SharedPreferences 一个小例子

127 篇文章 0 订阅
//main.xml,主布局
<?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" >  
  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/name" />  
    <EditText  
        android:id="@+id/webname"   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" />  
    <TextView  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="@string/age" />  
    <EditText   
        android:id="@+id/age"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" />  
    <LinearLayout   
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:orientation="horizontal" >  
              
        <Button   
            android:id="@+id/saveButton"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="@string/save" />  
        <Button   
            android:id="@+id/restorationData"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="@string/huifu" />  
      
    </LinearLayout>  
</LinearLayout>  
//strings.xml常量字符串
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  
    <string name="hello">Hello SharedPreferencesActivity!</string>  
    <string name="app_name">软件参数保存</string>  
    <string name="name">网名</string>  
    <string name="age">年龄</string>  
    <string name="save">保存参数</string>  
    <string name="success">保存成功</string>  
    <string name="huifu">恢复参数</string>  
  
</resources>  


//SharedPreferencesActivity.java 处理类
package sn.len.sharedpreferences;  
  
import android.app.Activity;  
import android.content.Context;  
import android.content.SharedPreferences;  
import android.content.SharedPreferences.Editor;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.Toast;  
  
public class SharedPreferencesActivity extends Activity   
{  
    private  EditText name=null;  
    private EditText age=null;  
    @Override  
    public void onCreate(Bundle savedInstanceState)   
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        name=(EditText)findViewById(R.id.webname);  
        age=(EditText)findViewById(R.id.age);  
        Button saveButton=(Button)findViewById(R.id.saveButton);  
        Button restorationButton=(Button)findViewById(R.id.restorationData);  
        //写入信息   
        saveButton.setOnClickListener  
        (  
                new View.OnClickListener()  
                {  
                    @Override  
                    public void onClick(View v)   
                    {  
  
                        String web_name=name.getText().toString();  
                        String true_age=age.getText().toString();  
                        SharedPreferences preferences=getSharedPreferences("softinfo",Context.MODE_WORLD_READABLE);  
                        Editor edit=preferences.edit();  
                        edit.putString("name", web_name);  
                        edit.putInt("age",new Integer(true_age));  
                        edit.commit();  
                        Toast.makeText(SharedPreferencesActivity.this, R.string.success,Toast.LENGTH_LONG).show();  
                          
                    }  
                }  
        );  
        //读取信息   
        restorationButton.setOnClickListener  
        (  
                new View.OnClickListener()  
                {  
                      
                    @Override  
                    public void onClick(View v)   
                    {  
                        SharedPreferences ferences=getSharedPreferences("softinfo",0);  
                        String true_name=ferences.getString("name", "");  
                        int true_age=ferences.getInt("age", 20);  
                        name.setText(true_name);  
                        age.setText(String.valueOf(true_age));  
                    }  
                }  
        );  
    }  
}  

//运行结果
点击保存按钮,成功后消除内容,再点恢复数据,也就是重新读取XML的值放在EditText中


//其它App中,访问这个APP的信息,如下代码

package sn.len.others;  
  
import android.content.Context;  
import android.content.SharedPreferences;  
import android.content.pm.PackageManager.NameNotFoundException;  
import android.test.AndroidTestCase;  
import android.util.Log;  
  
public class SharPreferencesSoftinfoTest extends AndroidTestCase   
{  
    private static final String TAG="SharePreferencesContent";  
    public void testAccessSharedPreferences() throws NameNotFoundException  
    {  
        //通过本应该程序的上下文来建想要访问App的上下文对象   
        //sn.len.sharedpreferences 存放softinfo.xml那个包名称   
        //CONTEXT_IGNORE_SECURITY 取消跨App的的访问安全   
        Context accessSharePreferences=getContext().createPackageContext("sn.len.sharedpreferences",Context.CONTEXT_IGNORE_SECURITY);  
        SharedPreferences shared=accessSharePreferences.getSharedPreferences("softinfo", Context.MODE_PRIVATE);  
        String name=shared.getString("name", "");  
        int age=shared.getInt("age", 18);  
        Log.i(TAG, "name:"+name+"age:"+age);  
    }  
}  




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值