当前位置:   article > 正文

Android Studio初学者实例:SharedPreferences 登录/注册模块实现_android studio 登录注册页面

android studio 登录注册页面

该实验是紧接上一期使用SP登录、记住密码的实验,主要实现是除了上次登陆以外,登录以后在主界面有一系列假数据,可以通过右上角进行排序选择,以下是实验效果:

 示例:好友名排序效果

首先是登陆界面以及逻辑代码,在逻辑代码中实现记住密码(勾选选择框后)

activity_login3.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#E6E6E6"
  7. android:orientation="vertical"
  8. android:padding="10dp">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_marginTop="15dp"
  13. android:background="@android:color/white"
  14. android:orientation="horizontal">
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:padding="10dp"
  19. android:text="账号:"
  20. android:textColor="#000"
  21. android:textSize="20sp" />
  22. <EditText
  23. android:id="@+id/et_account"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_marginLeft="5dp"
  27. android:background="@null"
  28. android:padding="10dp" />
  29. </LinearLayout>
  30. <LinearLayout
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_marginTop="10dp"
  34. android:background="@android:color/white"
  35. android:orientation="horizontal">
  36. <TextView
  37. android:id="@+id/tv_password"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:padding="10dp"
  41. android:text="密码:"
  42. android:textColor="#000"
  43. android:textSize="20sp" />
  44. <EditText
  45. android:id="@+id/et_password"
  46. android:layout_width="match_parent"
  47. android:layout_height="wrap_content"
  48. android:layout_marginLeft="5dp"
  49. android:background="@null"
  50. android:inputType="textPassword"
  51. android:padding="10dp" />
  52. </LinearLayout>
  53. <CheckBox
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:text="记住密码"
  57. android:id="@+id/cb"/>
  58. <LinearLayout
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:orientation="horizontal">
  62. <Button
  63. android:id="@+id/btn_login"
  64. android:layout_weight="1"
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content"
  67. android:layout_marginTop="25dp"
  68. android:background="#3C8DC4"
  69. android:text="登录"
  70. android:textColor="@android:color/white"
  71. android:textSize="20sp" />
  72. <Button
  73. android:id="@+id/btn_reg"
  74. android:layout_weight="1"
  75. android:layout_width="match_parent"
  76. android:layout_height="wrap_content"
  77. android:layout_marginTop="25dp"
  78. android:background="#3C8DC4"
  79. android:text="注册"
  80. android:textColor="@android:color/white"
  81. android:textSize="20sp" />
  82. </LinearLayout>
  83. </LinearLayout>

 登陆成功后跳转到主界面,主界面由一个ListView和右上角抽屉Menu构成

以下是主界面的界面代码

activity_main60.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. xmlns:app="http://schemas.android.com/apk/res-auto"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. tools:context=".MainActivity60">
  9. <ListView
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:id="@+id/lv"/>
  13. </LinearLayout>

 因为用到了ListView,所以需要一个item的布局代码:

frin_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. android:padding="10dp">
  7. <ImageView
  8. android:layout_width="50dp"
  9. android:layout_height="50dp"
  10. android:src="@drawable/txlogo" />
  11. <LinearLayout
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:orientation="vertical">
  15. <LinearLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:orientation="horizontal">
  19. <TextView
  20. android:id="@+id/id"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_weight="1"
  24. android:text="ID:1"
  25. android:textSize="20dp" />
  26. <TextView
  27. android:id="@+id/name"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_weight="1"
  31. android:text="姓名:李"
  32. android:textSize="20dp" />
  33. </LinearLayout>
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:orientation="horizontal">
  38. <TextView
  39. android:id="@+id/sex"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:layout_weight="1"
  43. android:text="性别:女"
  44. android:textSize="20dp" />
  45. <TextView
  46. android:id="@+id/phone"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:layout_weight="1"
  50. android:text="电话:123123"
  51. android:textSize="20dp" />
  52. </LinearLayout>
  53. </LinearLayout>
  54. </LinearLayout>

 menu的代码:(在res下创建menu文件夹在文件夹中创建menu.xml)

menu.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item
  4. android:id="@+id/menu_order"
  5. android:icon="@android:drawable/ic_menu_help"
  6. android:title="排序"
  7. />
  8. </menu>

 接下来是主界面的逻辑代码,需要处理几件事:设置好友初始化数据、设置ListView适配器以及显示数据、初始化Menu、点击Menu能够跳转到选择排序的设置界面

值得注意的是适配器采用内部类的方式写在了MainActivity60中

以下是逻辑代码:

首先是登录界面的逻辑代码(上期内容)

SharedHelper

  1. import android.content.Context;
  2. import android.content.SharedPreferences;
  3. import android.widget.Toast;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class SharedHelper {
  7. private Context mContext;
  8. public SharedHelper() {
  9. }
  10. public SharedHelper(Context mContext) {
  11. this.mContext = mContext;
  12. }
  13. //定义一个保存数据的方法
  14. public void save(String username, String passwd) {
  15. SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
  16. SharedPreferences.Editor editor = sp.edit();
  17. editor.putString("username", username);
  18. editor.putString("passwd", passwd);
  19. editor.commit();
  20. }
  21. //定义一个读取SP文件的方法
  22. public Map<String, String> read() {
  23. Map<String, String> data = new HashMap<String, String>();
  24. SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
  25. data.put("username", sp.getString("username", ""));
  26. data.put("passwd", sp.getString("passwd", ""));
  27. return data;
  28. }
  29. //清空SP
  30. public void clear(){
  31. SharedPreferences sp = mContext.getSharedPreferences("mysp", Context.MODE_PRIVATE);
  32. SharedPreferences.Editor editor = sp.edit();
  33. editor.clear();
  34. editor.commit();
  35. }
  36. }

LoginActivity

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.os.Bundle;
  5. import android.preference.PreferenceManager;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.CheckBox;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11. import java.util.Map;
  12. public class LoginActivity extends AppCompatActivity {
  13. private EditText et_account, et_password;
  14. private Button btn_login,reg_login;
  15. private CheckBox cb;
  16. private SharedHelper sh;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_login3);
  21. et_account = (EditText)findViewById(R.id.et_account);
  22. et_password = (EditText)findViewById(R.id.et_password);
  23. cb = (CheckBox)findViewById(R.id.cb);
  24. //判断SharedPreferences中是否保存name的值
  25. sh = new SharedHelper(LoginActivity.this);
  26. Map<String, String> data = sh.read();
  27. et_account.setText(data.get("username"));
  28. et_password.setText(data.get("passwd"));
  29. if(!et_account.getText().toString().isEmpty()){
  30. cb.setChecked(true);
  31. }
  32. btn_login = findViewById(R.id.btn_login);
  33. btn_login.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View v) {
  36. String username = et_account.getText().toString();
  37. String passwd = et_password.getText().toString();
  38. if (username.isEmpty() || passwd.isEmpty()) {
  39. Toast.makeText(LoginActivity.this, "请输入完整账号密码", Toast.LENGTH_SHORT).show();
  40. } else {
  41. if (cb.isChecked()) {
  42. sh.save(username, passwd);
  43. } else {
  44. sh.clear();
  45. }
  46. Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
  47. Intent intent=new Intent(LoginActivity.this,MainActivity60.class);
  48. startActivity(intent);
  49. }
  50. }
  51. });
  52. }
  53. }

MainActivity60

  1. import androidx.annotation.NonNull;
  2. import androidx.annotation.Nullable;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.os.Bundle;
  7. import android.preference.PreferenceManager;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.BaseAdapter;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.Comparator;
  18. import java.util.List;
  19. public class MainActivity60 extends AppCompatActivity {
  20. List<Friend> friends=new ArrayList<>();
  21. ListView lv;
  22. myDapter myAdapter;
  23. @Override
  24. protected void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. setContentView(R.layout.activity_main60);
  27. if(friends.size()<1){
  28. friends.add(new Friend("1","张三","123123","男"));
  29. friends.add(new Friend("2","李四","321","男"));
  30. friends.add(new Friend("3","王五","43534","男"));
  31. friends.add(new Friend("4","喜羊羊","4364","男"));
  32. friends.add(new Friend("5","美羊羊","74568","女"));
  33. friends.add(new Friend("6","懒羊羊","76864","男"));
  34. friends.add(new Friend("7","沸羊羊","3234","男"));
  35. friends.add(new Friend("8","慢羊羊","5436","男"));
  36. }
  37. myAdapter=new myDapter();
  38. lv=findViewById(R.id.lv);
  39. lv.setAdapter(myAdapter);
  40. }
  41. @Override
  42. protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
  43. //排序
  44. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
  45. int sortBy = pref.getInt("sort", 1);
  46. //创建比较器
  47. Comparator<Friend> comparator = null;
  48. switch(sortBy){
  49. case 1:
  50. comparator = new SortById();
  51. break;
  52. case 2:
  53. comparator = new SortByName();
  54. break;
  55. case 3:
  56. comparator = new SortByPhone();
  57. break;
  58. case 4:
  59. comparator = new SortBySex();
  60. break;
  61. }
  62. Collections.sort(friends, comparator);
  63. myAdapter.notifyDataSetChanged();
  64. super.onActivityResult(requestCode, resultCode, data);
  65. }
  66. @Override
  67. public boolean onCreateOptionsMenu(Menu menu) {
  68. getMenuInflater().inflate(R.menu.menu,menu);
  69. return super.onCreateOptionsMenu(menu);
  70. }
  71. @Override
  72. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  73. switch (item.getItemId()){
  74. case R.id.menu_order:
  75. Intent intent1 = new Intent(MainActivity60.this,SettingActivity.class);
  76. startActivityForResult(intent1, 0);
  77. break;
  78. }
  79. return super.onOptionsItemSelected(item);
  80. }
  81. class myDapter extends BaseAdapter{
  82. @Override
  83. public int getCount() {
  84. return friends.size();
  85. }
  86. @Override
  87. public Object getItem(int position) {
  88. return friends.get(position);
  89. }
  90. @Override
  91. public long getItemId(int position) {
  92. return position;
  93. }
  94. @Override
  95. public View getView(int position, View convertView, ViewGroup parent) {
  96. View view=View.inflate(MainActivity60.this,R.layout.frin_item,null);
  97. TextView id=view.findViewById(R.id.id);
  98. TextView name=view.findViewById(R.id.name);
  99. TextView sex=view.findViewById(R.id.sex);
  100. TextView phone=view.findViewById(R.id.phone);
  101. id.setText("ID:"+friends.get(position).getId());
  102. name.setText("姓名:"+friends.get(position).getName());
  103. sex.setText("性别:"+friends.get(position).getSex());
  104. phone.setText("电话:"+friends.get(position).getPhone());
  105. return view;
  106. }
  107. }
  108. }

 还差一个用于选择排序的Activity,界面由RadioGroup以及RadioButton构成,在逻辑代码中则是在SP中设置选择的排序并返回主界面。

activity_setting.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:orientation="vertical"
  8. tools:context=".SettingActivity">
  9. <RadioGroup
  10. android:id="@+id/rgSortMethod"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:orientation="vertical">
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="排序方式" />
  18. <RadioButton
  19. android:id="@+id/rdoSortById"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:text="按ID排序" />
  23. <RadioButton
  24. android:id="@+id/rdoSortByName"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:text="按好友名字排序" />
  28. <RadioButton
  29. android:id="@+id/rdoSortByPhone"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:text="按电话号码排序" />
  33. <RadioButton
  34. android:id="@+id/rdoSortBySex"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:text="按性别排序" />
  38. </RadioGroup>
  39. <Button
  40. android:id="@+id/btn"
  41. android:layout_width="match_parent"
  42. android:layout_height="wrap_content"
  43. android:text="确定" />
  44. </LinearLayout>

 SettingActivity

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.os.Bundle;
  5. import android.preference.PreferenceManager;
  6. import android.view.View;
  7. import android.widget.RadioGroup;
  8. public class SettingActivity extends AppCompatActivity {
  9. RadioGroup rgSortBy;
  10. SharedPreferences pref;
  11. int sortBy;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_setting);
  16. rgSortBy = (RadioGroup) findViewById(R.id.rgSortMethod);
  17. pref = PreferenceManager.getDefaultSharedPreferences(this);
  18. sortBy = pref.getInt("sort", 1);
  19. switch (sortBy) {
  20. case 1:
  21. rgSortBy.check(R.id.rdoSortById);
  22. break;
  23. case 2:
  24. rgSortBy.check(R.id.rdoSortByName);
  25. break;
  26. case 3:
  27. rgSortBy.check(R.id.rdoSortByPhone);
  28. break;
  29. case 4:
  30. rgSortBy.check(R.id.rdoSortBySex);
  31. break;
  32. }
  33. rgSortBy.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  34. @Override
  35. public void onCheckedChanged(RadioGroup group, int checkedId) {
  36. switch (checkedId) {
  37. case R.id.rdoSortById:
  38. sortBy = 1;
  39. break;
  40. case R.id.rdoSortByName:
  41. sortBy = 2;
  42. break;
  43. case R.id.rdoSortByPhone:
  44. sortBy = 3;
  45. break;
  46. case R.id.rdoSortBySex:
  47. sortBy = 4;
  48. break;
  49. }
  50. }
  51. });
  52. findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. //保存排序方式
  56. pref.edit().putInt("sort", sortBy).commit();
  57. Intent intent = new Intent();
  58. Bundle bundle = new Bundle();
  59. bundle.putString("type", "order");
  60. intent.putExtras(bundle);
  61. intent.setClass(SettingActivity.this, MainActivity.class);
  62. setResult(RESULT_OK, intent);
  63. finish();
  64. }
  65. });
  66. }
  67. }

另外,前面用到一个实体类,这里给出

Friend

  1. public class Friend {
  2. private String id;
  3. private String name;
  4. private String phone;
  5. private String sex;
  6. public String getId() {
  7. return id;
  8. }
  9. public void setId(String id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public String getPhone() {
  19. return phone;
  20. }
  21. public void setPhone(String phone) {
  22. this.phone = phone;
  23. }
  24. public String getSex() {
  25. return sex;
  26. }
  27. public void setSex(String sex) {
  28. this.sex = sex;
  29. }
  30. public Friend(String id, String name, String phone, String sex) {
  31. this.id = id;
  32. this.name = name;
  33. this.phone = phone;
  34. this.sex = sex;
  35. }
  36. }

还有几个比较器:(结构都大致相同的)

SortById
  1. import java.text.Collator;
  2. import java.util.Comparator;
  3. import java.util.Locale;
  4. public class SortByName implements Comparator<Friend> {
  5. @Override
  6. public int compare(Friend f1, Friend f2) {
  7. Comparator comparator = Collator.getInstance(Locale.CHINESE); //这里是中文,如果不是中文,//里面的Locale.CHINESE可以去掉
  8. return comparator.compare(f1.getId(), f2.getId());
  9. }
  10. }
SortByName
  1. import java.text.Collator;
  2. import java.util.Comparator;
  3. import java.util.Locale;
  4. public class SortByName implements Comparator<Friend> {
  5. @Override
  6. public int compare(Friend f1, Friend f2) {
  7. Comparator comparator = Collator.getInstance(Locale.CHINESE); //这里是中文,如果不是中文,//里面的Locale.CHINESE可以去掉
  8. return comparator.compare(f1.getName(), f2.getName());
  9. }
  10. }
SortByPhone
  1. import java.text.Collator;
  2. import java.util.Comparator;
  3. import java.util.Locale;
  4. public class SortByPhone implements Comparator<Friend> {
  5. @Override
  6. public int compare(Friend f1, Friend f2) {
  7. Comparator comparator = Collator.getInstance(Locale.CHINESE); //这里是中文,如果不是中文,//里面的Locale.CHINESE可以去掉
  8. return comparator.compare(f1.getPhone(), f2.getPhone());
  9. }
  10. }
SortBySex
  1. import java.text.Collator;
  2. import java.util.Comparator;
  3. import java.util.Locale;
  4. public class SortBySex implements Comparator<Friend> {
  5. @Override
  6. public int compare(Friend f1, Friend f2) {
  7. Comparator comparator = Collator.getInstance(Locale.CHINESE); //这里是中文,如果不是中文,//里面的Locale.CHINESE可以去掉
  8. return comparator.compare(f1.getSex(), f2.getSex());
  9. }
  10. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/537024
推荐阅读
相关标签
  

闽ICP备14008679号