前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SharePreference存储

SharePreference存储

作者头像
Dream城堡
发布2018-12-18 16:56:14
4350
发布2018-12-18 16:56:14
举报
文章被收录于专栏:Spring相关Spring相关

SharePreference存储

1.新建SharePreferenceTest项目
2.修改activity_main.xml:
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sharepreferencetest.MainActivity">


    <Button
        android:id="@+id/button"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存数据按钮"
        android:layout_weight="1"
        />


    <Button
        android:id="@+id/get_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取出数据按钮"
        android:layout_weight="1"

        />
 
</LinearLayout>
3.修改MainActivity
代码语言:javascript
复制
package com.example.sharepreferencetest;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

 

    private static final String TAG = "MainActivity";
 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        


        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences.Editor edit = getSharedPreferences(
                        "data", MODE_PRIVATE
                ).edit();

                edit.putString("name", "张三");
                edit.putInt("age", 15);
                edit.putBoolean("old", false);
                edit.apply();


            }
        });

        Button getBut = (Button) findViewById(R.id.get_data);

        getBut.setOnClickListener(

                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
                        String name = pref.getString("name", null);
                        int age = pref.getInt("age", 0);
                        boolean old = pref.getBoolean("old", false);
                        Log.d(TAG, "onClick: " + name);
//                        Toast.makeText(context, name + " " + age + " " + old, Toast.LENGTH_LONG).show();
                    }

                });

    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SharePreference存储
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档