Android编程6:SharedPreferences文件存储操作

本文介绍了一个使用SharedPreferences存储和读取数据的简单示例。示例中创建了一个Android应用,通过SharedPreferences保存了用户ID和姓名,并在应用界面上显示这些信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文博客链接:http://blog.youkuaiyun.com/jdh99,作者:jdh,转载请注明.

SharedPreferences类提供了一种简单的文件存储功能,像程序的配置文件可以通过它来实现。

源代码:

package com.test.sharedpreferences; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.TextView; public class test_sharedpreferences extends Activity { SharedPreferences file; SharedPreferences.Editor editor; TextView text; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得配置文件 file = getSharedPreferences("test_config", 0); editor = file.edit(); //写入配置:Id:10000 Name:Jdh editor.putInt("Id", 10000); editor.putString("Name", "Jdh"); editor.commit(); //读取配置 int id = 0; id = file.getInt("Id", 0); String str; str = file.getString("Name", null); text = (TextView)findViewById(R.id.textView1); text.setText("Id:" + id + " Name:" + str); } } 效果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值