SharedPreferences存储
实例–实现登录功能
package com.example.practice16;
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;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String zhanghao = "123";
private String mima = "456";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText usernameET = findViewById(R.id.e1);
final EditText passwordET = findViewById(R.id.e2);
Button b1 = findViewById(R.id.b1);
//获取SharedPreferences对象
final SharedPreferences sp = getSharedPreferences("hello",MODE_PRIVATE);
//实现自动登录功能
String username = sp.getString("username",null);//获取账号信息
String password = sp.getString("password",null);//获取密码
if(username!=null && password!=null){
if(username.equals(zhanghao)&&password.equals(mima)){
Intent intent = new Intent(MainActivity.this,MessageActivity.class);
startActivity(intent);
}
}
//实现手动登录并存储账号和密码
else {
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String in_username = usernameET.getText().toString();
String in_password = passwordET.getText().toString();
SharedPreferences.Editor editor = sp.edit();
if(in_username.equals(zhanghao)&&in_password.equals(mima)){
editor.putString("username",in_username);//保存账号
editor.putString("password",in_password);//保存密码
editor.commit();//提交信息
Intent intent = new Intent(MainActivity.this,MessageActivity.class);
startActivity(intent);
Toast.makeText(MainActivity.this,"已保存账号和密码",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"账号和密码错误",Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
内部存储
package com.example.practice17;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
byte[] buffer = null;//定义保存数据的数组
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_save = findViewById(R.id.b1);
Button btn_cancel = findViewById(R.id.b2);
final EditText etext = findViewById(R.id.e1);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//保存填写的备忘信息
FileOutputStream fos = null; //声明文件输出流
String text = etext.getText().toString();//获取输入备忘信息
try {
fos = openFileOutput("memo",MODE_PRIVATE);//获取文件输出流对象
try {
fos.write(text.getBytes());
fos.flush();//清除缓存
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (fos != null) {
try {
fos.close();//关闭输出流
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
//读取保存的备忘信息
FileInputStream fis = null; //声明文件输入流对象
try {
fis = openFileInput("memo");//获取文件输入流对象
buffer=new byte[fis.available()];//实例化字节数组
fis.read(buffer);//从输入流中读取数据
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fis!=null) {
try {
fis.close();//关闭输入流对象
String data = new String(buffer);//把字节数组中的数据转换成字符串
etext.setText(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();//退出应用
}
});
}
}
外部存储
package com.example.practice17;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
byte[] buffer = null;//定义保存数据的数组
File file; //声明一个文件对象,用来指定外部存储文件的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_save = findViewById(R.id.b1);
Button btn_cancel = findViewById(R.id.b2);
final EditText etext = findViewById(R.id.e1);
file = new File(Environment.getExternalStorageDirectory(),"Text.text"); //实例化文件对象
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//保存填写的备忘信息
FileOutputStream fos = null; //声明文件输出流
String text = etext.getText().toString();//获取输入备忘信息
try {
fos = new FileOutputStream(file);
try {
fos.write(text.getBytes());
fos.flush();//清除缓存
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (fos != null) {
try {
fos.close();//关闭输出流
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
//读取保存的备忘信息
FileInputStream fis = null; //声明文件输入流对象
try {
fis = new FileInputStream(file);//获取文件输入流对象
buffer=new byte[fis.available()];//实例化字节数组
fis.read(buffer);//从输入流中读取数据
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fis!=null) {
try {
fis.close();//关闭输入流对象
String data = new String(buffer);//把字节数组中的数据转换成字符串
etext.setText(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();//退出应用
}
});
}
}
数据库存储----使用代码操作SQLite
。。。。。。
Content Provider