这期采用SharedPreferences存储QQ登录信息即QQ用户的账号密码
上一期使用文件存储的方式,又分为内部与外部
SharedPreferences有存储的格式限制,例如Set、String、int等,需要去了解一下
以下是实验的效果:
打开APP自动获取SharedPreferences存储的账号密码,显示在控件上
勾选记住密码,点击登录可以进行保存账号密码
首先是用于SharedPreferences保存数据、读取数据的工具类(比较懒,参考了一下W3cshool):SharedHelper
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;
import java.util.HashMap;
import java.util.Map;
public class SharedHelper {
private Context mContext;
public SharedHelper() {
}
public SharedHelper(Context mContext) {
this.mContext = mContext;
}
//定义一个保存数据的方法
public void save(String username, String passwd) {
SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", username);
editor.putString("passwd", passwd);
editor.commit();
}
//定义一个读取SP文件的方法
public Map<String, String> read() {
Map<String, String> data = new HashMap<String, String>();
SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
data.put("username", sp.getString("username", ""));
data.put("passwd", sp.getString("passwd", ""));
return data;
}
//清空SP
public void clear(){
SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.commit();
}
}
界面代码:
注意:我头像使用的第三方库以实现圆角头像,你可以需要改成ImageView即可
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:orientation="vertical"
android:padding="10dp">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_marginTop="100dp"
android:id="@+id/imageView1"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:src="@drawable/a111"
app:riv_border_color="#333333"
app:riv_border_width="2dip"
app:riv_corner_radius="30dip"
app:riv_mutate_background="true"
app:riv_oval="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="账号:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="密码:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:inputType="textPassword"
android:padding="10dp" />
</LinearLayout>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:id="@+id/cb"/>
<Button
android:onClick="function"
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#3C8DC4"
android:text="登录"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
activity逻辑代码:
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.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Map;
public class MainActivity57 extends AppCompatActivity {
private EditText et_account, et_password;
private Button btn_login;
private CheckBox cb;
private SharedHelper sh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main57);
et_account = findViewById(R.id.et_account);
et_password = findViewById(R.id.et_password);
cb = findViewById(R.id.cb);
sh = new SharedHelper(MainActivity57.this);
Map<String, String> data = sh.read();
et_account.setText(data.get("username"));
et_password.setText(data.get("passwd"));
if(!et_account.getText().toString().isEmpty()){
cb.setChecked(true);
}
btn_login = findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = et_account.getText().toString();
String passwd = et_password.getText().toString();
if (username.isEmpty() || passwd.isEmpty()) {
Toast.makeText(MainActivity57.this, "请输入完整账号密码", Toast.LENGTH_SHORT).show();
} else {
if (cb.isChecked()) {
sh.save(username, passwd);
} else {
sh.clear();
}
Toast.makeText(MainActivity57.this, "登陆成功", Toast.LENGTH_SHORT).show();
}
}
});
}
}
后续解析会在后续补充,急需的话可以在评论区催一催