找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2308|回复: 0
打印 上一主题 下一主题
收起左侧

android SharedPreferences存储数据

[复制链接]
跳转到指定楼层
楼主
ID:435645 发表于 2020-12-17 21:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
MainActivity.java:
package com.example.android9;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences preferences = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加载布局文件
        //获取ui控件
        Button btn_set=findViewById(R.id.btn_set);
        //按钮点击事件
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建意图
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);//开启意图
            }
        });
    }
    //重写onStart()函数
    @Override
    protected void onStart() {
        super.onStart();//继承
        //指定该SharedPreferences数据只能被本应用程序读写
        preferences = getSharedPreferences("crazyit", Context.MODE_PRIVATE);
        //读取字符数据
        String user = preferences.getString("user", "");
        //获取textview控件
        TextView tx = findViewById(R.id.tx);
        //设置显示内容
        tx.setText("欢迎 " + user + " 来到我的家园");
    }
}package com.example.android9;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences preferences = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加载布局文件
        //获取ui控件
        Button btn_set=findViewById(R.id.btn_set);
        //按钮点击事件
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建意图
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);//开启意图
            }
        });
    }
    //重写onStart()函数
    @Override
    protected void onStart() {
        super.onStart();//继承
        //指定该SharedPreferences数据只能被本应用程序读写
        preferences = getSharedPreferences("crazyit", Context.MODE_PRIVATE);
        //读取字符数据
        String user = preferences.getString("user", "");
        //获取textview控件
        TextView tx = findViewById(R.id.tx);
        //设置显示内容
        tx.setText("欢迎 " + user + " 来到我的家园");
    }
}
MainActivity2.java:
package com.example.android9;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity2 extends AppCompatActivity {
    private SharedPreferences preferences = null;
    private SharedPreferences.Editor editor = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局文件
        setContentView(R.layout.activity_main2);
        //利用SharedPreferences读取数据并显示
        preferences = getSharedPreferences("crazyit", Context.MODE_PRIVATE);
        //获取haredPreferences.Editor对象,尝试写数据
        editor = preferences.edit();
        //为“确定”按钮绑定监听器
        Button btn_ok = findViewById(R.id.btn);
        final EditText tv = findViewById(R.id.tv);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user = tv.getText().toString();
                editor.putString("user", user);
                editor.apply();
                finish();
            }
        });
    }
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity" >

    <TextView
        android:id="@+id/tx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="43dp"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="31dp"
        android:textSize="22dp"
        android:text="欢迎  来到我的家园" />
    <Button
        android:id="@+id/btn_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tx"
        android:layout_marginLeft="130dp"
        android:textSize="22dp"
        android:text="参数设置"/>
</RelativeLayout>
activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity2">
    <TextView
        android:id="@+id/tx2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入用户名:"
        android:textSize="28dp" />

    <EditText
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tx2"
        android:inputType="text"
        android:ems="9"
        android:textColorHighlight="@color/colorAccent"
        android:textSize="28dp" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:textSize="28dp"
        android:layout_marginLeft="100dp"
        android:text="确定"/>

</RelativeLayout>

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表