SharedPreferences的简单使用

package com.example.demousercount;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.widget.TextView;

public class DemoUserCount extends Activity {
    //声明一个SharedPreferences
    SharedPreferences preferences; 
    public TextView mytv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo_user_count);
        mytv = (TextView)findViewById(R.id.mytv);
        //声明一个preferences
        preferences = getSharedPreferences("count", MODE_WORLD_READABLE);
        // 读preferences中的count数据
        int count = preferences.getInt("count", 0);
        //将int转化为String
        String counts = String.valueOf(count);
        //显示程序以前使用的次数
        mytv.setText(counts);
        //编辑preferences中的内容
        Editor editor = preferences.edit();
        //将count加1
        editor.putInt("count", ++count);
        //实施生效
        editor.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_demo_user_count, menu);
        return true;
    }

}

最主要的是标颜色的代码

layout布局中只有一个textview

转载于:https://www.cnblogs.com/merryjd/archive/2012/12/25/2832213.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值