,而且优势就是sp读写起来会比文件更加方便,所以像这种情况我们就可以用sp的形式来存储
=======================================================================================
因为sp是存储在data/data/包名目录下的,所以我们不需要动态申请权限
1.添加button并添加点击事件
核心代码
button.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
1.activity代码(拿到SharedPreferences,再添加数据)
package com.hnucm.a_test13;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVi
ew(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences(“user”,MODE_PRIVATE);//拿到 sharedPreferences 的引用 MODE_PRIVATE表示只能被本应用读写,不能被其他应用读写
SharedPreferences.Editor editor=sharedPreferences.edit();//拿到编辑对象
editor.putBoolean(“isLogin”,true);//添加数据
editor.commit();
}
});
}
}
2.运行,点击按钮,刷新一下data/data/包名目录,会发现我们的数据被保存到了一个文件夹
1.核心代码
package com.hnucm.a_test13;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences=getSharedPreferences(“user”,MODE_PRIVATE);//拿到 sharedPreferences 的引用 MODE_PRIVATE表示只能被本应用读写,不能被其他应用读写
boolean isLogin = sharedPreferences.getBoolean(“isLogin”, false);//当key不存在时,默认返回defValue的值
Log.i(“MainActivity”,"isLogin "+isLogin);
Toast.makeText(MainActivity.this,“”+isLogin,Toast.LENGTH_SHORT).show();
}
});
}
}
2.运行结果
三、SharedPreferences案例-qq登录demo
=============================================================================================
需求分析:qq登录第一次登录需要输入账号密码,如果登录成功,第二次启动qq可以直接跳到主界面,不需要先跳转到登录页面
我们需要先创建三个activity
因为我们需要一个启动跳转页面,一个登录页面,一个主界面,所以需要三个页面
1.默认第一个见到的页面是MainActivity,我们需要把LaunchActivity设置为我们第一个见到的activity
2.并且去掉应用的标题框
1.给启动跳转页面设置背景图片(图片如下)
2.并且设置延时跳转(需要判断用户是否登录过)
3.LaunchActivity代码
package com.hnucm.a_test13;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class LaunchActivity extends AppCompatActivity {
@SuppressLint(“HandlerLeak”)
Handler mHandler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
SharedPreferences sharedPreferences= getSharedPreferences(“user”, MODE_PRIVATE);
boolean islogin = sharedPreferences.getBoolean(“islogin”,false);//如果读不到文件或者当key不存在时 返回false
//判断用户是否登录过
if (islogin) { //如果登录过 跳转到 MainActivity
Intent intent=new Intent(LaunchActivity.this,MainActivity.class);
startActivity(intent);
}else { //如果没有登录过 跳转到 LoginActivity
Intent intent=new Intent(LaunchActivity.this,LoginActivity.class);
startActivity(intent);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
mHandler.sendEmptyMessageDelayed(1,3000);//3秒后发送消息
}
}
4.运行结果
1.(简单设置一下)
核心代码
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout 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=“.LoginActivity”>
<TextView
android:id=“@+id/textView2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“148dp”
android:text=“这是登录界面”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toTopOf=“parent” />
<Button
android:id=“@+id/button2”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“124dp”
android:text=“登录”
app:layout_constraintEnd_toEndOf=“parent”
app:layout_constraintStart_toStartOf=“parent”
app:layout_constraintTop_toBottomOf=“@+id/textView2” />
</androidx.constraintlayout.widget.ConstraintLayout>
2.再到LoginActivity,设置登录界面点击按钮返回登录状态到文件
核心代码
package com.hnucm.a_test13;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class LoginActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
button=findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击按钮 代表登录成功
//获取SharedPreferences对象
SharedPreferences sharedPreferences=getSharedPreferences(“user”,MODE_PRIVATE);
//获取Editor对象的引用
SharedPreferences.Editor editor=sharedPreferences.edit();
//将登录的状态放入文件
editor.putBoolean(“islogin”,true);
editor.putString(“username”,“dd”);
//提交数据
editor.commit();
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
3.主界面页面
MainActivity代码
package com.hnucm.a_test13;
import androidx.appcompat.app.AppCompatActivity;
最后
小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
资料⬅专栏获取
g.csdnimg.cn/14407a9ad9124831a637606516ab26d8.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5aSP5bG_Xw==,size_20,color_FFFFFF,t_70,g_se,x_16)
MainActivity代码
package com.hnucm.a_test13;
import androidx.appcompat.app.AppCompatActivity;
最后
小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
[外链图片转存中…(img-cQsFRAm3-1718984303506)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
资料⬅专栏获取