当前位置:   article > 正文

【安卓app开发一】Android Studio + Bmob后端云实现注册&登录账号、密码找回、意见反馈及数据可视化_bmob连接网络

bmob连接网络

目录

前言

概览

Bmob后端云介绍

Bmob后端云与Android Studio配置

一、Bmob后端云

 二、Android Studio配置

工具类

一、User类

二、Suit类

三、Code类

实现类

 一、登录代码

 二、注册代码

 三、找回密码代码

 四、想法反馈代码


前言

        本项目尚在开发阶段,主要针对的是安卓用户,通过查阅开发文档以及借鉴其他博主,由于是个人开发,可能会在逻辑会有所缺漏,欢迎各位安卓使用者与开发者进行内测并提交反馈意见给我,进行探讨顺便带带我;

        本项目开发主题尚未决定,故本期向各位征集意见,可以通过安装该app进行注册登录,并写下您的想法提交反馈给我,后续将对收集的idea进行整合并决定方向,进行下一步的开发。

        ps:保证无病毒与无信息泄露!!!欢迎大家参与

源代码:

链接:https://pan.baidu.com/s/10ECb54ne_9bubuCLWLB-Kw?pwd=v2sh 
提取码:v2sh

安卓手机软件安装包:

链接:https://pan.baidu.com/s/1a2H5CKxKvtAjvnyOHvybUQ?pwd=np3w 
提取码:np3w


概览

        本期主要用到两个工具,Android Studio和Bmob后端云(后续介绍),在安卓客户端注册登陆并提交想法,最终在Bmob后端云中实现数据可视化,主要实现效果如下图所示

  • Android Studio

  • Bmob后端云

         Android Studio在此不做介绍了,以下是对Bmob后端云的介绍

Bmob后端云介绍

        本次采用Bmob后端云(Bmob后端云 (bmobapp.com))主要是其集成了数据库与服务器,对于很多Android/ios/wp个人移动开发者来说,开发一个具有网络功能的应用不是一件容易的事,不仅需要购买/租赁服务器,还必须掌握一门诸如Java/.net/php这类的服务器开发语言,每开发一款移动应用程序,就必须开发维护对应的服务器程序,鉴于此我目前水平有限,故先采用Bmob后端云进行开发,给应用软件快速添加一个安全灵活的后台管理系统,方便浏览终端保存的各种信息。


Bmob后端云与Android Studio配置

一、Bmob后端云

  • 创建应用

  • 创建表单,默认有一个_User表单(不用管),要新建一个User表单(名称要与后边安卓代码相同),数据将会在这个表单呈现

  •  获取key秘钥,后边需要用

 二、Android Studio配置

  •  AndroidManifest.xml:

        1、添加网络权限

        在上图位置添加下方代码以添加网络权限

  1. <!--允许联网 -->
  2. <uses-permission android:name="android.permission.INTERNET" />
  3. <!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息 -->
  4. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  5. <!--获取wifi网络状态的信息 -->
  6. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  7. <!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
  8. <uses-permission android:name="android.permission.WAKE_LOCK" />
  9. <!--获取sd卡写的权限,用于文件上传和下载-->
  10. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  11. <!--允许读取手机状态 用于创建BmobInstallation-->
  12. <uses-permission android:name="android.permission.READ_PHONE_STATE" />

        2、连接Bmob

         在上图位置添加下方代码,并将蓝色方框中的代码替换成红色箭头所指的自己代码的包名

  1. <provider
  2. android:name="cn.bmob.v3.util.BmobContentProvider"
  3. android:authorities="包名.BmobContentProvider">
  4. </provider>
  •  在Activity中对Bmob进行初始化,在OnCreat方法中添加下方代码并将你的应用ID替换成上方Bmob配置时获取的key密钥

Bmob.initialize(this,"你的应用ID");
  • 在build.gradle(Module:app)中添加包

  1. implementation 'io.github.bmob:android-sdk:3.9.4'
  2. implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
  3. implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
  4. implementation 'com.squareup.okhttp3:okhttp:4.8.1'
  5. implementation 'com.squareup.okio:okio:2.2.2'
  6. implementation 'com.google.code.gson:gson:2.8.5'

        至此,配置已全部完成,接下来是进行界面设计与逻辑代码编写环节


工具类

一、User类

        这个工具类的名称与前边在Bmob创建的表单名称相同,这个类继承于BmobObject,其属性相当于在表中的数据。

  1. package com.example.application.tool;
  2. import java.io.Serializable;
  3. import cn.bmob.v3.BmobObject;
  4. public class User extends BmobObject implements Serializable {
  5. private String Acctount;//手机账户
  6. private String Password;//密码
  7. private String Context;//反馈内容
  8. public String getAcctount() {
  9. return Acctount;
  10. }
  11. public void setAcctount(String acctount) {
  12. Acctount = acctount;
  13. }
  14. public String getPassword() {
  15. return Password;
  16. }
  17. public void setPassword(String password) {
  18. Password = password;
  19. }
  20. public String getContext() {
  21. return Context;
  22. }
  23. public void setContext(String context) {
  24. Context = context;
  25. }
  26. }

二、Suit类

        这个类主要是号码检测(是否符合格式)与密码检测(至少包含3个字母)

  1. package com.example.application.tool;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. public class Suit {
  5. //手机号码检测
  6. public static boolean PhoneSuit(String phone){
  7. String ChineseMainland = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" + "|(18[0-9])|(19[8,9]))\\d{8}$";
  8. String HongKong = "^(5|6|8|9)\\d{7}$";
  9. Matcher C = Pattern.compile(ChineseMainland).matcher(phone);
  10. Matcher H = Pattern.compile(HongKong).matcher(phone);
  11. return C.matches() || H.matches();
  12. }
  13. //密码检测(最少3个字母)
  14. public static boolean PasswordSuit(String password){
  15. char[] s = password.toCharArray();
  16. int count = 0 ;
  17. //检测所有的密码中有多少个字母
  18. for (int i = 0;i < s.length;i++){
  19. if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z')){
  20. count++;
  21. }
  22. }
  23. if (count >= 3){
  24. return true;
  25. }else {
  26. return false;
  27. }
  28. }
  29. }

三、Code类

        这个类主要是生成验证码图片,由于目前短信验证在资源上有数量限制,故采用验证码图片进行验证,在实现类中通过创建类对象调用类方法实现对验证码图片的生成。

  1. package com.example.application.tool;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import java.util.Random;
  7. public class Code {
  8. //随机数数组
  9. private static final char[] CHARS = {
  10. '2', '3', '4', '5', '6', '7', '8', '9',
  11. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm',
  12. 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  13. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  14. 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
  15. };
  16. private static Code bmpCode;
  17. public static Code getInstance() {
  18. if(bmpCode == null)
  19. bmpCode = new Code();
  20. return bmpCode;
  21. }
  22. //default settings
  23. //验证码默认随机数的个数
  24. private static final int DEFAULT_CODE_LENGTH = 4;
  25. //默认字体大小
  26. private static final int DEFAULT_FONT_SIZE = 25;
  27. //默认线条的条数
  28. private static final int DEFAULT_LINE_NUMBER = 5;
  29. //padding值
  30. private static final int BASE_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;
  31. //验证码的默认宽高
  32. private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40;
  33. //settings decided by the layout xml
  34. //canvas width and height
  35. private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
  36. //random word space and pading_top
  37. private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,
  38. base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;
  39. //number of chars, lines; font size
  40. private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;
  41. //variables
  42. private String code;
  43. private int padding_left, padding_top;
  44. private Random random = new Random();
  45. //验证码图片
  46. public Bitmap createBitmap() {
  47. padding_left = 0;
  48. Bitmap bp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  49. Canvas c = new Canvas(bp);
  50. code = createCode();
  51. c.drawColor(Color.WHITE);
  52. Paint paint = new Paint();
  53. paint.setAntiAlias(true);
  54. paint.setTextSize(font_size);
  55. //画验证码
  56. for (int i = 0; i < code.length(); i++) {
  57. randomTextStyle(paint);
  58. randomPadding();
  59. c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);
  60. }
  61. //画线条
  62. for (int i = 0; i < line_number; i++) {
  63. drawLine(c, paint);
  64. }
  65. //c.save(Canvas.ALL_SAVE_FLAG);//保存
  66. c.save();
  67. c.restore();
  68. return bp;
  69. }
  70. public String getCode() {
  71. return code;
  72. }
  73. //生成验证码
  74. private String createCode() {
  75. StringBuilder buffer = new StringBuilder();
  76. for (int i = 0; i < codeLength; i++) {
  77. buffer.append(CHARS[random.nextInt(CHARS.length)]);
  78. }
  79. return buffer.toString();
  80. }
  81. //画干扰线
  82. private void drawLine(Canvas canvas, Paint paint) {
  83. int color = randomColor();
  84. int startX = random.nextInt(width);
  85. int startY = random.nextInt(height);
  86. int stopX = random.nextInt(width);
  87. int stopY = random.nextInt(height);
  88. paint.setStrokeWidth(1);
  89. paint.setColor(color);
  90. canvas.drawLine(startX, startY, stopX, stopY, paint);
  91. }
  92. //生成随机颜色
  93. private int randomColor() {
  94. return randomColor(1);
  95. }
  96. private int randomColor(int rate) {
  97. int red = random.nextInt(256) / rate;
  98. int green = random.nextInt(256) / rate;
  99. int blue = random.nextInt(256) / rate;
  100. return Color.rgb(red, green, blue);
  101. }
  102. //随机生成文字样式,颜色,粗细,倾斜度
  103. private void randomTextStyle(Paint paint) {
  104. int color = randomColor();
  105. paint.setColor(color);
  106. paint.setFakeBoldText(random.nextBoolean()); //true为粗体,false为非粗体
  107. float skewX = random.nextInt(11) / 10;
  108. skewX = random.nextBoolean() ? skewX : -skewX;
  109. paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
  110. }
  111. //随机生成padding值
  112. private void randomPadding() {
  113. padding_left += base_padding_left + random.nextInt(range_padding_left);
  114. padding_top = base_padding_top + random.nextInt(range_padding_top);
  115. }
  116. }

实现类

 一、登录代码

  •  布局代码

  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="match_parent"
  7. android:background="@mipmap/fm"
  8. android:orientation="vertical"
  9. tools:context=".activity.MainActivity">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content">
  13. <TextView
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:fontFamily="serif"
  17. android:gravity="center"
  18. android:padding="80dp"
  19. android:text="Login"
  20. android:textColor="@color/white"
  21. android:textSize="50dp"
  22. >
  23. </TextView>
  24. </LinearLayout>
  25. <LinearLayout
  26. android:paddingHorizontal="15dp"
  27. android:orientation="horizontal"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content">
  30. <TextView
  31. android:layout_width="50dp"
  32. android:layout_height="match_parent"
  33. android:background="@mipmap/phone">
  34. </TextView>
  35. <EditText
  36. android:id="@+id/Login_Phone"
  37. android:hint="请输入手机号码"
  38. android:textSize="25dp"
  39. android:theme="@style/MyEditText"
  40. android:fontFamily="sans-serif-condensed-medium"
  41. android:textColorHint="#B0BEC5"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content"
  44. >
  45. </EditText>
  46. </LinearLayout>
  47. <LinearLayout
  48. android:paddingHorizontal="15dp"
  49. android:paddingTop="20dp"
  50. android:orientation="horizontal"
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content">
  53. <TextView
  54. android:layout_width="50dp"
  55. android:layout_height="match_parent"
  56. android:background="@mipmap/password">
  57. </TextView>
  58. <EditText
  59. android:id="@+id/Login_Password"
  60. android:hint="请输入密码"
  61. android:textSize="25dp"
  62. android:theme="@style/MyEditText"
  63. android:fontFamily="sans-serif-condensed-medium"
  64. android:textColorHint="#B0BEC5"
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content"
  67. >
  68. </EditText>
  69. </LinearLayout>
  70. <TextView
  71. android:id="@+id/Login"
  72. android:layout_marginTop="20dp"
  73. android:layout_gravity="center"
  74. android:background="@mipmap/jiantou"
  75. android:layout_width="80dp"
  76. android:layout_height="80dp">
  77. </TextView>
  78. <LinearLayout
  79. android:paddingHorizontal="20dp"
  80. android:layout_width="match_parent"
  81. android:layout_height="wrap_content">
  82. <TextView
  83. android:id="@+id/Login_Lose"
  84. android:layout_width="wrap_content"
  85. android:layout_height="match_parent"
  86. android:text="忘记密码?"
  87. android:textColor="#ECEFF1"
  88. android:textSize="25dp"
  89. >
  90. </TextView>
  91. <TextView
  92. android:id="@+id/Login_Resgister"
  93. android:layout_marginLeft="140dp"
  94. android:layout_width="wrap_content"
  95. android:layout_height="match_parent"
  96. android:text="注册账号"
  97. android:textColor="#ECEFF1"
  98. android:textSize="25dp"
  99. >
  100. </TextView>
  101. </LinearLayout>
  102. </LinearLayout>
  •  实现代码
  1. package com.example.application.activity;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9. import android.widget.Toast;
  10. import com.example.application.R;
  11. import com.example.application.tool.User;
  12. import java.util.List;
  13. import cn.bmob.v3.Bmob;
  14. import cn.bmob.v3.BmobQuery;
  15. import cn.bmob.v3.exception.BmobException;
  16. import cn.bmob.v3.listener.FindListener;
  17. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  18. private EditText login_phone;//输入收集号码
  19. private EditText login_password;//输入密码
  20. private TextView login;//登陆按钮
  21. private TextView login_lose;//忘记密码
  22. private TextView login_resgister;//注册账号
  23. private String phone;
  24. private String password;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. Bmob.initialize(this,"ebb1d24c01600c55b1e8be7f2625d7a7");
  30. initView();
  31. }
  32. //初始化控件
  33. private void initView() {
  34. login_phone = findViewById(R.id.Login_Phone);
  35. login_password = findViewById(R.id.Login_Password);
  36. login = findViewById(R.id.Login);
  37. login_lose = findViewById(R.id.Login_Lose);
  38. login_resgister = findViewById(R.id.Login_Resgister);
  39. //启动监听事件
  40. login.setOnClickListener(this);
  41. login_lose.setOnClickListener(this);
  42. login_resgister.setOnClickListener(this);
  43. }
  44. //点击事件
  45. @Override
  46. public void onClick(View view) {
  47. //添加意图,切换画面
  48. Intent intent = new Intent();
  49. //选择监听按钮触发机制
  50. switch (view.getId()){
  51. case R.id.Login:
  52. Getdata();
  53. //没有输入号码、密码判断为空则弹窗提示
  54. if (TextUtils.isEmpty(phone)){
  55. Toast.makeText(this,"请输入手机号码",Toast.LENGTH_SHORT).show();
  56. } else if (TextUtils.isEmpty(password)) {
  57. Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
  58. } else {
  59. BmobQuery<User> bmobQuery = new BmobQuery<>();
  60. bmobQuery.findObjects(new FindListener<User>() {
  61. @Override
  62. public void done(List<User> object, BmobException e) {
  63. //如果没有异常的话进行查询列表账号
  64. if(e == null){
  65. int count = 0;//计数
  66. //遍历
  67. for (User user : object) {
  68. //查询到与客户端输入的账号一致
  69. if (user.getAcctount().equals(phone)){
  70. //查询到与客户端输入的密码一致
  71. if (user.getPassword().equals(password)){
  72. intent.setClass(getApplicationContext(),First_interface.class);
  73. //传递对象,方便主界面获取到用户信息
  74. intent.putExtra("User",user);
  75. startActivity(intent);
  76. Toast.makeText(getApplicationContext(),"登陆成功",Toast.LENGTH_SHORT).show();
  77. break;
  78. }else {
  79. Toast.makeText(getApplicationContext(),"密码错误",Toast.LENGTH_SHORT).show();
  80. }
  81. }
  82. count++;
  83. }
  84. //遍历没有查询到,此时count++到object的数量
  85. if (count >= object.size()){
  86. Toast.makeText(getApplicationContext(),"账号不存在",Toast.LENGTH_SHORT).show();
  87. }
  88. } else {
  89. Toast.makeText(getApplicationContext(),"账号不存在",Toast.LENGTH_SHORT).show();
  90. }
  91. }
  92. });
  93. }
  94. break;
  95. case R.id.Login_Lose:
  96. intent.setClass(getApplicationContext(),Findpassword.class);
  97. startActivity(intent);
  98. break;
  99. case R.id.Login_Resgister:
  100. intent.setClass(getApplicationContext(),Resgister.class);
  101. startActivity(intent);
  102. break;
  103. }
  104. }
  105. //获取输入的号码与密码
  106. public void Getdata(){
  107. phone = login_phone.getText().toString().trim();
  108. password = login_password.getText().toString().trim();
  109. }
  110. }

 二、注册代码

  • 布局代码

  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="match_parent"
  7. android:background="@mipmap/fm"
  8. tools:context=".activity.Resgister"
  9. android:orientation="vertical">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content">
  13. <TextView
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:fontFamily="serif"
  17. android:gravity="center"
  18. android:padding="80dp"
  19. android:text="Register"
  20. android:textColor="@color/white"
  21. android:textSize="50dp"
  22. >
  23. </TextView>
  24. </LinearLayout>
  25. <LinearLayout
  26. android:paddingHorizontal="15dp"
  27. android:paddingTop="20dp"
  28. android:orientation="horizontal"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content">
  31. <TextView
  32. android:layout_width="50dp"
  33. android:layout_height="match_parent"
  34. android:background="@mipmap/phone">
  35. </TextView>
  36. <EditText
  37. android:id="@+id/Register_Phone"
  38. android:hint="请输入手机号码"
  39. android:textSize="25dp"
  40. android:theme="@style/MyEditText"
  41. android:fontFamily="sans-serif-condensed-medium"
  42. android:textColorHint="#B0BEC5"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. >
  46. </EditText>
  47. </LinearLayout>
  48. <LinearLayout
  49. android:paddingHorizontal="15dp"
  50. android:paddingTop="20dp"
  51. android:orientation="horizontal"
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content">
  54. <TextView
  55. android:layout_width="50dp"
  56. android:layout_height="match_parent"
  57. android:background="@mipmap/password">
  58. </TextView>
  59. <EditText
  60. android:id="@+id/Register_Password"
  61. android:hint="请输入密码"
  62. android:textSize="25dp"
  63. android:theme="@style/MyEditText"
  64. android:fontFamily="sans-serif-condensed-medium"
  65. android:textColorHint="#B0BEC5"
  66. android:layout_width="match_parent"
  67. android:layout_height="wrap_content"
  68. >
  69. </EditText>
  70. </LinearLayout>
  71. <LinearLayout
  72. android:paddingHorizontal="15dp"
  73. android:paddingTop="20dp"
  74. android:orientation="horizontal"
  75. android:layout_width="match_parent"
  76. android:layout_height="wrap_content">
  77. <TextView
  78. android:layout_weight="0.7"
  79. android:layout_width="0dp"
  80. android:layout_height="match_parent"
  81. android:background="@mipmap/message">
  82. </TextView>
  83. <EditText
  84. android:id="@+id/Register_SMS"
  85. android:hint="请输入验证码"
  86. android:textSize="25dp"
  87. android:theme="@style/MyEditText"
  88. android:fontFamily="sans-serif-condensed-medium"
  89. android:textColorHint="#B0BEC5"
  90. android:layout_weight="3"
  91. android:layout_width="0dp"
  92. android:layout_height="wrap_content"
  93. >
  94. </EditText>
  95. <ImageView
  96. android:id="@+id/Register_Paint"
  97. android:gravity="center"
  98. android:layout_weight="2"
  99. android:layout_width="0dp"
  100. android:textSize="20dp"
  101. android:layout_height="50dp"
  102. android:background="@color/white">
  103. </ImageView>
  104. </LinearLayout>
  105. <TextView
  106. android:id="@+id/Register"
  107. android:layout_marginTop="10dp"
  108. android:layout_width="100dp"
  109. android:layout_height="60dp"
  110. android:layout_gravity="center"
  111. android:background="@mipmap/register">
  112. </TextView>
  113. <TextView
  114. android:id="@+id/Login_Back"
  115. android:paddingRight="20dp"
  116. android:gravity="end"
  117. android:text="已有账号?去登录"
  118. android:textColor="#ECEFF1"
  119. android:textSize="25dp"
  120. android:layout_width="match_parent"
  121. android:layout_height="wrap_content"></TextView>
  122. </LinearLayout>
  •  实现代码
  1. package com.example.application.activity;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11. import com.example.application.R;
  12. import com.example.application.tool.Code;
  13. import com.example.application.tool.Suit;
  14. import com.example.application.tool.User;
  15. import java.util.List;
  16. import cn.bmob.v3.Bmob;
  17. import cn.bmob.v3.BmobQuery;
  18. import cn.bmob.v3.exception.BmobException;
  19. import cn.bmob.v3.listener.FindListener;
  20. import cn.bmob.v3.listener.SaveListener;
  21. public class Resgister extends AppCompatActivity implements View.OnClickListener {
  22. private static final String TAG = null;
  23. private EditText register_phone;//获取注册的手机号码
  24. private EditText register_password;//获取注册的密码
  25. private EditText register_sms;//输入验证码
  26. private TextView register;//注册
  27. private TextView login_back;//返回登陆
  28. private ImageView register_paint;//获取验证码
  29. private String phone;//客户端输入的手机号码
  30. private String passeword;//客户端输入的密码
  31. private String sms;//客户端输入的验证码
  32. private String realCode;//图形验证码
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. super.onCreate(savedInstanceState);
  36. setContentView(R.layout.activity_resgister);
  37. Bmob.initialize(this,"ebb1d24c01600c55b1e8be7f2625d7a7");
  38. initView();
  39. }
  40. //初始化控件
  41. private void initView() {
  42. //获取控件变量
  43. register_phone = findViewById(R.id.Register_Phone);
  44. register_password = findViewById(R.id.Register_Password);
  45. register_sms = findViewById(R.id.Register_SMS);
  46. register = findViewById(R.id.Register);
  47. login_back = findViewById(R.id.Login_Back);
  48. register_paint = findViewById(R.id.Register_Paint);
  49. //启动监听机制
  50. //将验证码用图片的形式显示出来
  51. register_paint.setImageBitmap(Code.getInstance().createBitmap());
  52. realCode = Code.getInstance().getCode().toLowerCase();
  53. register_paint.setOnClickListener(this);
  54. register.setOnClickListener(this);
  55. login_back.setOnClickListener(this);
  56. }
  57. //获取数据
  58. public void Getdata(){
  59. phone = register_phone.getText().toString().trim();
  60. passeword = register_password.getText().toString().trim();
  61. sms = register_sms.getText().toString().toLowerCase();
  62. }
  63. @Override
  64. public void onClick(View view) {
  65. switch (view.getId()){
  66. case R.id.Register_Paint:
  67. register_paint.setImageBitmap(Code.getInstance().createBitmap());
  68. realCode = Code.getInstance().getCode().toLowerCase();
  69. Log.v(TAG,"realCode"+realCode);
  70. break;
  71. case R.id.Login_Back:
  72. finish();
  73. break;
  74. case R.id.Register:
  75. Getdata();
  76. //输入限制条件:不能为空、号码以及密码符合格式、密码数量限制条件:6到16
  77. if (TextUtils.isEmpty(phone)){
  78. Toast.makeText(getApplicationContext(),"请输入手机号码",Toast.LENGTH_SHORT).show();
  79. } else if (TextUtils.isEmpty(passeword)) {
  80. Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_SHORT).show();
  81. } else if (TextUtils.isEmpty(sms)) {
  82. Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_SHORT).show();
  83. } else if (Suit.PhoneSuit(phone.trim()) != true) {
  84. Toast.makeText(getApplicationContext(),"请输入正确的手机号码",Toast.LENGTH_SHORT).show();
  85. } else if (Suit.PasswordSuit(passeword.trim()) != true) {
  86. Toast.makeText(getApplicationContext(),"密码最少包含3个字母",Toast.LENGTH_SHORT).show();
  87. } else if (passeword.length() < 6) {
  88. Toast.makeText(getApplicationContext(),"密码不得少于6位数",Toast.LENGTH_SHORT).show();
  89. } else if (passeword.length() > 16) {
  90. Toast.makeText(getApplicationContext(),"密码不得多于16位数",Toast.LENGTH_SHORT).show();
  91. }else {
  92. BmobQuery<User> bmobQuery = new BmobQuery<>();
  93. bmobQuery.findObjects(new FindListener<User>() {
  94. @Override
  95. public void done(List<User> object, BmobException e) {
  96. if (e == null){
  97. int count = 0;//判断是否查询到尾--遍历
  98. //查询判断用户是否已经存在
  99. for (User user : object){
  100. if (user.getAcctount().equals(phone)){
  101. register_phone.setText("");
  102. Toast.makeText(getApplicationContext(),"该用户已经注册过",Toast.LENGTH_SHORT).show();
  103. finish();
  104. break;
  105. }
  106. count++;
  107. }
  108. //查询到尾没有
  109. if (count == object.size()){
  110. //判断验证码是否正确
  111. if (sms.equals(realCode)) {
  112. Toast.makeText(getApplicationContext(), "验证码正确", Toast.LENGTH_SHORT).show();
  113. //将用户信息存入bmob云端
  114. final User user = new User();
  115. user.setAcctount(phone);
  116. user.setPassword(passeword);
  117. user.save(new SaveListener<String>() {
  118. @Override
  119. public void done(String s, BmobException e) {
  120. if (e == null){
  121. Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
  122. finish();
  123. }else {
  124. Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_SHORT).show();
  125. }
  126. }
  127. });
  128. } else {
  129. register_sms.setText("");
  130. Toast.makeText(getApplicationContext(), "验证码错误", Toast.LENGTH_SHORT).show();
  131. }
  132. }
  133. }else {
  134. Toast.makeText(Resgister.this, "该用户不存在", Toast.LENGTH_SHORT).show();
  135. }
  136. }
  137. });
  138. }
  139. break;
  140. }
  141. }
  142. }

 三、找回密码代码

  • 布局代码

  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="match_parent"
  7. android:orientation="vertical"
  8. android:background="@mipmap/fm"
  9. tools:context=".activity.Findpassword">
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content">
  13. <TextView
  14. android:layout_marginTop="80dp"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:fontFamily="serif"
  18. android:gravity="center"
  19. android:padding="20dp"
  20. android:text="FindPassword"
  21. android:textColor="@color/white"
  22. android:textSize="50dp"
  23. >
  24. </TextView>
  25. </LinearLayout>
  26. <LinearLayout
  27. android:layout_marginTop="20dp"
  28. android:paddingHorizontal="15dp"
  29. android:orientation="horizontal"
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content">
  32. <TextView
  33. android:layout_width="50dp"
  34. android:layout_height="match_parent"
  35. android:background="@mipmap/phone">
  36. </TextView>
  37. <EditText
  38. android:id="@+id/Find_Phone"
  39. android:hint="请输入手机号码"
  40. android:textSize="25dp"
  41. android:theme="@style/MyEditText"
  42. android:fontFamily="sans-serif-condensed-medium"
  43. android:textColorHint="#B0BEC5"
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. >
  47. </EditText>
  48. </LinearLayout>
  49. <LinearLayout
  50. android:paddingHorizontal="15dp"
  51. android:paddingTop="20dp"
  52. android:orientation="horizontal"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content">
  55. <TextView
  56. android:layout_weight="0.7"
  57. android:layout_width="0dp"
  58. android:layout_height="match_parent"
  59. android:background="@mipmap/message">
  60. </TextView>
  61. <EditText
  62. android:id="@+id/Find_SMS"
  63. android:hint="请输入验证码"
  64. android:textSize="25dp"
  65. android:theme="@style/MyEditText"
  66. android:fontFamily="sans-serif-condensed-medium"
  67. android:textColorHint="#B0BEC5"
  68. android:layout_weight="3"
  69. android:layout_width="0dp"
  70. android:layout_height="wrap_content"
  71. >
  72. </EditText>
  73. <ImageView
  74. android:id="@+id/Find_Image"
  75. android:gravity="center"
  76. android:layout_weight="2"
  77. android:layout_width="0dp"
  78. android:textSize="20dp"
  79. android:layout_height="50dp"
  80. android:background="@color/white">
  81. </ImageView>
  82. </LinearLayout>
  83. <LinearLayout
  84. android:paddingHorizontal="15dp"
  85. android:paddingTop="20dp"
  86. android:orientation="horizontal"
  87. android:layout_width="match_parent"
  88. android:layout_height="wrap_content">
  89. <TextView
  90. android:layout_width="50dp"
  91. android:layout_height="match_parent"
  92. android:background="@mipmap/password">
  93. </TextView>
  94. <EditText
  95. android:id="@+id/Find_Password"
  96. android:hint="请输入密码"
  97. android:textSize="25dp"
  98. android:theme="@style/MyEditText"
  99. android:fontFamily="sans-serif-condensed-medium"
  100. android:textColorHint="#B0BEC5"
  101. android:layout_width="match_parent"
  102. android:layout_height="wrap_content"
  103. >
  104. </EditText>
  105. </LinearLayout>
  106. <TextView
  107. android:id="@+id/Findword"
  108. android:layout_marginTop="10dp"
  109. android:layout_gravity="center"
  110. android:background="@mipmap/queren"
  111. android:layout_width="80dp"
  112. android:layout_height="80dp">
  113. </TextView>
  114. <TextView
  115. android:id="@+id/Login_Go"
  116. android:layout_marginRight="35dp"
  117. android:text="去登陆"
  118. android:textSize="25dp"
  119. android:textColor="#ECEFF1"
  120. android:gravity="end"
  121. android:layout_width="match_parent"
  122. android:layout_height="wrap_content">
  123. </TextView>
  124. </LinearLayout>
  •  实现代码
  1. package com.example.application.activity;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12. import com.example.application.R;
  13. import com.example.application.tool.Code;
  14. import com.example.application.tool.Suit;
  15. import com.example.application.tool.User;
  16. import java.util.List;
  17. import cn.bmob.v3.Bmob;
  18. import cn.bmob.v3.BmobQuery;
  19. import cn.bmob.v3.exception.BmobException;
  20. import cn.bmob.v3.listener.FindListener;
  21. import cn.bmob.v3.listener.UpdateListener;
  22. public class Findpassword extends AppCompatActivity implements View.OnClickListener {
  23. private static final String TAG = null;
  24. private EditText find_phone;//手机号码
  25. private EditText find_sms;//验证码
  26. private EditText find_password;//重设密码
  27. private ImageView find_image;//验证码图片
  28. private TextView findword;//完成
  29. private TextView login_go;//去登陆
  30. private String realCode;
  31. private String phone;
  32. private String sms;
  33. private String password;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_findpassword);
  38. Bmob.initialize(this,"ebb1d24c01600c55b1e8be7f2625d7a7");
  39. initView();
  40. }
  41. //初始化控件
  42. private void initView() {
  43. //控件变量
  44. find_phone = findViewById(R.id.Find_Phone);
  45. find_sms = findViewById(R.id.Find_SMS);
  46. find_password = findViewById(R.id.Find_Password);
  47. find_image = findViewById(R.id.Find_Image);
  48. findword = findViewById(R.id.Findword);
  49. login_go = findViewById(R.id.Login_Go);
  50. //监听机制
  51. find_image.setImageBitmap(Code.getInstance().createBitmap());
  52. realCode = Code.getInstance().getCode().toLowerCase();
  53. find_image.setOnClickListener(this);
  54. findword.setOnClickListener(this);
  55. login_go.setOnClickListener(this);
  56. }
  57. @Override
  58. public void onClick(View view) {
  59. Intent intent = new Intent();
  60. switch (view.getId()){
  61. case R.id.Find_Image:
  62. find_image.setImageBitmap(Code.getInstance().createBitmap());
  63. realCode = Code.getInstance().getCode().toLowerCase();
  64. Log.v(TAG,"realCode"+realCode);
  65. break;
  66. case R.id.Findword:
  67. Getdata();
  68. //判断不能为空
  69. if (TextUtils.isEmpty(phone)){
  70. Toast.makeText(getApplicationContext(),"请输入手机号码",Toast.LENGTH_SHORT).show();
  71. } else if (TextUtils.isEmpty(password)) {
  72. Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_SHORT).show();
  73. } else if (TextUtils.isEmpty(sms)) {
  74. Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_SHORT).show();
  75. } else if (Suit.PhoneSuit(phone.trim()) != true) {
  76. Toast.makeText(getApplicationContext(),"请输入正确的手机号码",Toast.LENGTH_SHORT).show();
  77. } else if (Suit.PasswordSuit(password.trim()) != true) {
  78. Toast.makeText(getApplicationContext(),"密码最少包含3个字母",Toast.LENGTH_SHORT).show();
  79. } else if (password.length() < 6) {
  80. Toast.makeText(getApplicationContext(),"密码不得少于6位数",Toast.LENGTH_SHORT).show();
  81. } else if (password.length() > 16) {
  82. Toast.makeText(getApplicationContext(), "密码不得多于16位数", Toast.LENGTH_SHORT).show();
  83. }else {
  84. BmobQuery<User> bmobQuery = new BmobQuery<>();
  85. bmobQuery.findObjects(new FindListener<User>() {
  86. @Override
  87. public void done(List<User> object, BmobException e) {
  88. if (e == null){
  89. int count = 0;//判断是否查询到尾--遍历
  90. //查询判断用户是否已经存在
  91. for (User user : object){
  92. if (user.getAcctount().equals(phone)){
  93. Toast.makeText(getApplicationContext(),"该用户存在",Toast.LENGTH_SHORT).show();
  94. //判断验证码是否正确
  95. if (sms.equals(realCode)) {
  96. //云端数据更新
  97. User user1 = new User();
  98. user1.setPassword(password);
  99. user1.update(user.getObjectId(), new UpdateListener() {
  100. @Override
  101. public void done(BmobException e) {
  102. if (e == null) {
  103. Toast.makeText(getApplicationContext(),"密码修改成功",Toast.LENGTH_SHORT).show();
  104. }else {
  105. Toast.makeText(getApplicationContext(),"修改失败"+"\n"+"错误代码:"+e.getErrorCode(),Toast.LENGTH_LONG).show();
  106. }
  107. }
  108. });
  109. finish();
  110. break;
  111. }else {
  112. find_sms.setText("");
  113. Toast.makeText(getApplicationContext(), "验证码错误", Toast.LENGTH_SHORT).show();
  114. }
  115. }
  116. count++;
  117. }
  118. //查询到尾没有
  119. if (count >= object.size()){
  120. Toast.makeText(getApplicationContext(),"该用户不存在",Toast.LENGTH_SHORT).show();
  121. }
  122. }else {
  123. Toast.makeText(getApplicationContext(),"该用户不存在",Toast.LENGTH_SHORT).show();
  124. }
  125. }
  126. });
  127. }
  128. break;
  129. case R.id.Login_Go:
  130. finish();
  131. break;
  132. }
  133. }
  134. //获取输入的数据
  135. private void Getdata() {
  136. phone = find_phone.getText().toString().trim();
  137. sms = find_sms.getText().toString().toLowerCase();
  138. password = find_password.getText().toString().trim();
  139. }
  140. }

 四、想法反馈代码

  • 布局代码

  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="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".activity.First_interface">
  9. <EditText
  10. android:id="@+id/Idea"
  11. android:paddingTop="20dp"
  12. android:paddingHorizontal="20dp"
  13. android:hint="请输入您的想法"
  14. android:gravity="left"
  15. android:textSize="30dp"
  16. android:background="@null"
  17. android:textColorHint="#B0BEC5"
  18. android:layout_width="match_parent"
  19. android:layout_height="0dp"
  20. android:layout_weight="13">
  21. </EditText>
  22. <Button
  23. android:id="@+id/submit"
  24. android:text="提交"
  25. android:gravity="center|bottom"
  26. android:textSize="30dp"
  27. android:background="#2196F3"
  28. android:layout_width="match_parent"
  29. android:layout_height="0dp"
  30. android:layout_weight="1">
  31. </Button>
  32. </LinearLayout>
  • 实现代码
    1. package com.example.application.activity;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.content.Intent;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.Button;
    7. import android.widget.EditText;
    8. import android.widget.Toast;
    9. import com.example.application.R;
    10. import com.example.application.tool.User;
    11. import java.util.List;
    12. import cn.bmob.v3.Bmob;
    13. import cn.bmob.v3.BmobQuery;
    14. import cn.bmob.v3.exception.BmobException;
    15. import cn.bmob.v3.listener.FindListener;
    16. import cn.bmob.v3.listener.SaveListener;
    17. import cn.bmob.v3.listener.UpdateListener;
    18. public class First_interface extends AppCompatActivity implements View.OnClickListener {
    19. private EditText idea;//编辑框
    20. private Button submit;//提交按钮
    21. private String context;//编辑框内容
    22. @Override
    23. protected void onCreate(Bundle savedInstanceState) {
    24. super.onCreate(savedInstanceState);
    25. setContentView(R.layout.activity_first_interface);
    26. Bmob.initialize(this,"ebb1d24c01600c55b1e8be7f2625d7a7");
    27. initView();
    28. }
    29. //初始化控件
    30. private void initView() {
    31. //定义控件
    32. idea = findViewById(R.id.Idea);
    33. submit = findViewById(R.id.submit);
    34. //监听
    35. submit.setOnClickListener(this);
    36. }
    37. public void Getdata(){
    38. context = idea.getText().toString().trim();
    39. }
    40. @Override
    41. public void onClick(View view) {
    42. switch (view.getId()){
    43. case R.id.submit:
    44. Getdata();
    45. //传递对象
    46. Intent intent = getIntent();
    47. User user = (User) intent.getSerializableExtra("User");
    48. BmobQuery<User> bmobQuery = new BmobQuery<>();
    49. bmobQuery.findObjects(new FindListener<User>() {
    50. @Override
    51. public void done(List<User> list, BmobException e) {
    52. if(e == null){
    53. System.out.println("1");
    54. for (User user1 : list) {
    55. //查询到一样
    56. if(user.getAcctount().equals(user1.getAcctount())){
    57. //向云端提交数据
    58. user1.setContext(context);
    59. user1.update(user1.getObjectId(), new UpdateListener() {
    60. @Override
    61. public void done(BmobException e) {
    62. if (e == null){
    63. Toast.makeText(First_interface.this, "提交成功", Toast.LENGTH_SHORT).show();
    64. }else {
    65. Toast.makeText(First_interface.this, "提交失败", Toast.LENGTH_SHORT).show();
    66. }
    67. }
    68. });
    69. break;
    70. }
    71. }
    72. }
    73. }
    74. });
    75. break;
    76. }
    77. }
    78. }

            至此,本期项目已经全部实现,尚有不足之处,后续将继续完善补充,欢迎各位下载试玩!

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

闽ICP备14008679号