Android如何实现简易登陆注册实例源码

猿友 2021-06-22 16:36:00 浏览数 (3763)
反馈

今天给大家带来Android如何实现简易登陆注册实例源码,希望能对各位W3Cschool学员有所帮助。

Android制作登录和注册

activity_login

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:id="@+id/tv_username"
  10. android:layout_width="wrap_content"
  11. android:layout_height="40dp"
  12. android:layout_marginStart="40dp"
  13. android:layout_marginTop="100dp"
  14. android:gravity="center_vertical"
  15. android:text="账号:"
  16. app:layout_constraintLeft_toLeftOf="parent"
  17. app:layout_constraintTop_toTopOf="parent" />
  18. <TextView
  19. android:id="@+id/tv_password"
  20. android:layout_width="wrap_content"
  21. android:layout_height="40dp"
  22. android:layout_marginStart="40dp"
  23. android:gravity="center_vertical"
  24. android:text="密码:"
  25. app:layout_constraintLeft_toLeftOf="parent"
  26. app:layout_constraintTop_toBottomOf="@id/tv_username" />
  27. <EditText
  28. android:id="@+id/ed_username"
  29. android:layout_width="300dp"
  30. android:layout_height="40dp"
  31. android:layout_marginTop="100dp"
  32. app:layout_constraintLeft_toRightOf="@id/tv_username"
  33. app:layout_constraintTop_toTopOf="parent" />
  34. <EditText
  35. android:id="@+id/ed_password"
  36. android:layout_width="300dp"
  37. android:inputType="textPassword"
  38. android:layout_height="40dp"
  39. app:layout_constraintLeft_toRightOf="@id/tv_password"
  40. app:layout_constraintTop_toBottomOf="@id/ed_username" />
  41. <Button
  42. android:id="@+id/bu_login"
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_marginTop="300dp"
  46. android:text="登录"
  47. app:layout_constraintLeft_toLeftOf="parent"
  48. app:layout_constraintRight_toLeftOf="@id/bu_register"
  49. app:layout_constraintTop_toTopOf="parent" />
  50. <Button
  51. android:id="@+id/bu_register"
  52. android:layout_width="wrap_content"
  53. android:layout_height="wrap_content"
  54. android:layout_marginTop="300dp"
  55. android:text="注册"
  56. app:layout_constraintLeft_toRightOf="@id/bu_login"
  57. app:layout_constraintRight_toRightOf="parent"
  58. app:layout_constraintTop_toTopOf="parent" />
  59. </androidx.constraintlayout.widget.ConstraintLayout>

LoginActivity

  1. package com.jld.exam;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.widget.EditText;
  6. import android.widget.Toast;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. public class LoginActivity extends AppCompatActivity {
  9. EditText ed_username;
  10. EditText ed_password;
  11. Button bu_login;
  12. Button bu_register;
  13. String username;
  14. String password;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_login);
  19. ed_username = findViewById(R.id.ed_username);
  20. ed_password = findViewById(R.id.ed_password);
  21. bu_login = findViewById(R.id.bu_login);
  22. bu_register = findViewById(R.id.bu_register);
  23. //登录按钮监听
  24. bu_login.setOnClickListener(v -> {
  25. username = ed_username.getText().toString();
  26. password = ed_password.getText().toString();
  27. //登陆的简单逻辑
  28. if (username.equals("")) {
  29. Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
  30. } else if (password.equals("")) {
  31. Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
  32. } else if (!username.equals("root") || !password.equals("123456")) {
  33. Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
  34. } else {
  35. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  36. startActivity(intent);
  37. }
  38. });
  39. //注册按钮监听
  40. bu_register.setOnClickListener(v -> {
  41. Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  42. startActivity(intent);
  43. });
  44. }
  45. }

activity_main

  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="vertical">
  6. <TextView
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:text="商品列表"
  10. android:layout_marginBottom="30dp"
  11. android:gravity="center_horizontal"/>
  12. <androidx.recyclerview.widget.RecyclerView
  13. android:id="@+id/recycler"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content" />
  16. </LinearLayout>

MainActivity

  1. package com.jld.exam;
  2. import android.graphics.Color;
  3. import android.os.Bundle;
  4. import android.widget.LinearLayout;
  5. import androidx.appcompat.app.AppCompatActivity;
  6. import androidx.recyclerview.widget.GridLayoutManager;
  7. import androidx.recyclerview.widget.RecyclerView;
  8. public class MainActivity extends AppCompatActivity {
  9. RecyclerViewAdapter recyclerViewAdapter;
  10. private final int[] icno = {R.drawable.clock, R.drawable.signal, R.drawable.box,
  11. R.drawable.second, R.drawable.elephone, R.drawable.ff, R.drawable.notebook, R.drawable.mark, R.drawable.yx,
  12. R.drawable.shop, R.drawable.theme, R.drawable.xl,};
  13. private final String[] name = {"时钟", "信号", "宝箱", "秒钟", "大象", "FF", "记事本", "书签", "印象", "商店", "主题", "迅雷"};
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);//GridLayoutManager
  18. GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 3);//创建recyclerview
  19. RecyclerView recyclerView = findViewById(R.id.recycler);//设定布局管理器
  20. recyclerView.setLayoutManager(gridLayoutManager);//创建适配器
  21. recyclerViewAdapter = new RecyclerViewAdapter(icno, name);
  22. recyclerView.setAdapter(recyclerViewAdapter);//设定适配器
  23. }
  24. }

activity_register

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
  7. tools:context=".RegisterActivity">
  8. <TextView
  9. android:id="@+id/textView0"
  10. android:layout_width="match_parent"
  11. android:layout_height="60dp"
  12. android:gravity="center"
  13. android:text="用户注册"
  14. android:textSize="30sp"
  15. app:layout_constraintTop_toTopOf="parent" />
  16. <TextView
  17. android:id="@+id/textView1"
  18. android:layout_width="100dp"
  19. android:layout_height="40dp"
  20. android:drawableStart="@drawable/account"
  21. android:gravity="center_vertical"
  22. android:text="用户名:"
  23. app:layout_constraintLeft_toLeftOf="parent"
  24. app:layout_constraintTop_toBottomOf="@id/textView0" />
  25. <EditText
  26. android:id="@+id/editText1"
  27. android:layout_width="300dp"
  28. android:layout_height="40dp"
  29. android:layout_weight="10"
  30. android:autofillHints=""
  31. android:gravity="center_horizontal"
  32. android:inputType="text"
  33. app:layout_constraintLeft_toRightOf="@id/textView1"
  34. app:layout_constraintTop_toBottomOf="@id/textView0" />
  35. <TextView
  36. android:id="@+id/textView2"
  37. android:layout_width="100dp"
  38. android:layout_height="40dp"
  39. android:layout_weight="2"
  40. android:drawableStart="@drawable/password"
  41. android:gravity="center_vertical"
  42. android:text="新密码:"
  43. app:layout_constraintLeft_toLeftOf="parent"
  44. app:layout_constraintTop_toBottomOf="@id/textView1" />
  45. <EditText
  46. android:id="@+id/editText2"
  47. android:layout_width="300dp"
  48. android:layout_height="40dp"
  49. android:layout_weight="10"
  50. android:autofillHints=""
  51. android:gravity="center_horizontal"
  52. android:inputType="textPassword"
  53. app:layout_constraintLeft_toRightOf="@id/textView2"
  54. app:layout_constraintTop_toBottomOf="@id/editText1" />
  55. <TextView
  56. android:id="@+id/textView5"
  57. android:layout_width="100dp"
  58. android:layout_height="40dp"
  59. android:drawableStart="@drawable/phone"
  60. android:gravity="center_vertical"
  61. android:text="手机电话:"
  62. app:layout_constraintLeft_toLeftOf="parent"
  63. app:layout_constraintTop_toBottomOf="@id/textView2" />
  64. <EditText
  65. android:id="@+id/editText5"
  66. android:layout_width="300dp"
  67. android:layout_height="40dp"
  68. android:layout_weight="10"
  69. android:autofillHints=""
  70. android:gravity="center_horizontal"
  71. android:inputType="phone"
  72. app:layout_constraintLeft_toRightOf="@id/textView5"
  73. app:layout_constraintTop_toBottomOf="@id/editText2" />
  74. <TextView
  75. android:id="@+id/textView7"
  76. android:layout_width="100dp"
  77. android:layout_height="40dp"
  78. android:drawableStart="@drawable/email"
  79. android:gravity="center_vertical"
  80. android:text="E_mail:"
  81. app:layout_constraintLeft_toLeftOf="parent"
  82. app:layout_constraintTop_toBottomOf="@id/editText5" />
  83. <EditText
  84. android:id="@+id/editText7"
  85. android:layout_width="300dp"
  86. android:layout_height="40dp"
  87. android:layout_weight="10"
  88. android:autofillHints=""
  89. android:gravity="center_horizontal"
  90. android:inputType="textEmailAddress"
  91. app:layout_constraintLeft_toRightOf="@id/textView7"
  92. app:layout_constraintTop_toBottomOf="@id/editText5" />
  93. <TextView
  94. android:id="@+id/textView8"
  95. android:layout_width="100dp"
  96. android:layout_height="40dp"
  97. android:drawableStart="@drawable/gender"
  98. android:gravity="center_vertical"
  99. android:text="性别:"
  100. app:layout_constraintLeft_toLeftOf="parent"
  101. app:layout_constraintTop_toBottomOf="@id/textView7" />
  102. <RadioGroup
  103. android:id="@+id/radioGroup8"
  104. android:layout_width="300dp"
  105. android:layout_height="40dp"
  106. android:layout_weight="10"
  107. android:gravity="center_vertical"
  108. android:orientation="horizontal"
  109. app:layout_constraintLeft_toRightOf="@id/textView8"
  110. app:layout_constraintTop_toBottomOf="@id/editText7">
  111. <RadioButton
  112. android:id="@+id/radioButton1"
  113. android:layout_width="wrap_content"
  114. android:layout_height="wrap_content"
  115. android:text="女" />
  116. <RadioButton
  117. android:id="@+id/radioButton2"
  118. android:layout_width="wrap_content"
  119. android:layout_height="wrap_content"
  120. android:text="男" />
  121. </RadioGroup>
  122. <Button
  123. android:id="@+id/button1"
  124. android:layout_width="wrap_content"
  125. android:layout_height="wrap_content"
  126. android:layout_marginTop="40dp"
  127. android:text="保存"
  128. app:layout_constraintEnd_toStartOf="@id/button2"
  129. app:layout_constraintStart_toStartOf="parent"
  130. app:layout_constraintTop_toBottomOf="@id/radioGroup8" />
  131. <Button
  132. android:id="@+id/button2"
  133. android:layout_width="wrap_content"
  134. android:layout_height="wrap_content"
  135. android:layout_marginTop="40dp"
  136. android:text="取消"
  137. app:layout_constraintEnd_toEndOf="parent"
  138. app:layout_constraintStart_toEndOf="@id/button1"
  139. app:layout_constraintTop_toBottomOf="@id/radioGroup8" />
  140. </androidx.constraintlayout.widget.ConstraintLayout>

RegisterActivity

  1. package com.jld.exam;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. public class RegisterActivity extends AppCompatActivity {
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_register);
  9. }
  10. }

recyc_item

  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="wrap_content"
  5. android:orientation="vertical">
  6. <ImageView
  7. android:id="@+id/iv_image"
  8. android:layout_width="60dp"
  9. android:layout_height="60dp"
  10. android:layout_gravity="center_horizontal"
  11. android:contentDescription="TODO"
  12. android:scaleType="centerInside"
  13. android:src="@drawable/ic_launcher_background" />
  14. <TextView
  15. android:id="@+id/tv_desc"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:gravity="center"
  19. android:hint="销售价格"
  20. android:textSize="14sp" />
  21. </LinearLayout>

RecyclerViewAdapter

  1. package com.jld.exam;
  2. import android.view.LayoutInflater;
  3. import android.view.View;
  4. import android.view.ViewGroup;
  5. import android.widget.ImageView;
  6. import android.widget.TextView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
  10. private final int[] icno;
  11. private final String[] desc;
  12. public RecyclerViewAdapter(int[] icno, String[] desc) {
  13. this.icno = icno;
  14. this.desc = desc;
  15. }
  16. @NonNull
  17. @Override
  18. public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  19. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyc_item, parent, false);
  20. return new ViewHolder(view);
  21. }
  22. @Override
  23. public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  24. holder.imageView.setImageResource(icno[position]);
  25. holder.textView.setText(desc[position]);
  26. }
  27. @Override
  28. public int getItemCount() {
  29. return icno.length;
  30. }
  31. //ViewHolder
  32. public static class ViewHolder extends RecyclerView.ViewHolder {
  33. View item;
  34. ImageView imageView;
  35. TextView textView;
  36. public ViewHolder(@NonNull View itemView) {
  37. super(itemView);
  38. item = itemView;
  39. imageView = itemView.findViewById(R.id.iv_image);
  40. textView = itemView.findViewById(R.id.tv_desc);
  41. }
  42. }
  43. }

到此这篇关于Android如何实现简易登陆注册实例源码文章就写到这了。

0 人点赞