• 个人冲刺二--体温五


    loginActivity.java

    复制代码
    package com.example.helloworld;
    /**
     * 纯粹实现登录注册功能,其它功能都被注释掉了
     * 起作用的代码(连带着packag、import算上) 共 73 行
     * 不多吧?
     */
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import java.util.ArrayList;
    
    /**
     * 此类 implements View.OnClickListener 之后,
     * 就可以把onClick事件写到onCreate()方法之外
     * 这样,onCreate()方法中的代码就不会显得很冗余
     */
    public class loginActivity extends AppCompatActivity implements View.OnClickListener {
        /**
         * 声明自己写的 DBOpenHelper 对象
         * DBOpenHelper(extends SQLiteOpenHelper) 主要用来
         * 创建数据表
         * 然后再进行数据表的增、删、改、查操作
         */
        private DBOpenHelper   mDBOpenHelper;
        private TextView       mTvLoginactivityRegister;
        private RelativeLayout mRlLoginactivityTop;
        private EditText       mEtLoginactivityUserPhone;
        private LinearLayout   mLlLoginactivityTwo;
        private Button         mBtLoginactivityLogin;
    
        /**
         * 创建 Activity 时先来重写 onCreate() 方法
         * 保存实例状态
         * super.onCreate(savedInstanceState);
         * 设置视图内容的配置文件
         * setContentView(R.layout.activity_login);
         * 上面这行代码真正实现了把视图层 View 也就是 layout 的内容放到 Activity 中进行显示
         * 初始化视图中的控件对象 initView()
         * 实例化 DBOpenHelper,待会进行登录验证的时候要用来进行数据查询
         * mDBOpenHelper = new DBOpenHelper(this);
         */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
    
            initView();
    
            mDBOpenHelper = new DBOpenHelper(this);
        }
    
        /**
         * onCreaete()中大的布局已经摆放好了,接下来就该把layout里的东西
         * 声明、实例化对象然后有行为的赋予其行为
         * 这样就可以把视图层View也就是layout 与 控制层 Java 结合起来了
         */
        private void initView() {
            // 初始化控件
            mBtLoginactivityLogin = findViewById(R.id.bt_loginactivity_login);
            mTvLoginactivityRegister = findViewById(R.id.tv_loginactivity_register);
            mRlLoginactivityTop = findViewById(R.id.rl_loginactivity_top);
            mEtLoginactivityUserPhone = findViewById(R.id.et_loginactivity_phone);
            mLlLoginactivityTwo = findViewById(R.id.ll_loginactivity_two);
    
            // 设置点击事件监听器
            mBtLoginactivityLogin.setOnClickListener(this);
            mTvLoginactivityRegister.setOnClickListener(this);
        }
    
        public void onClick(View view) {
            switch (view.getId()) {
                // 跳转到注册界面
                case R.id.tv_loginactivity_register:
                    startActivity(new Intent(this, RegisterActivity.class));
                    finish();
                    break;
                /**
                 * 登录验证:
                 *
                 * 从EditText的对象上获取文本编辑框输入的数据,并把左右两边的空格去掉
                 *  String name = mEtLoginactivityUsername.getText().toString().trim();
                 *  String password = mEtLoginactivityPassword.getText().toString().trim();
                 *  进行匹配验证,先判断一下用户名密码是否为空,
                 *  if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password))
                 *  再进而for循环判断是否与数据库中的数据相匹配
                 *  if (name.equals(user.getName()) && password.equals(user.getPassword()))
                 *  一旦匹配,立即将match = true;break;
                 *  否则 一直匹配到结束 match = false;
                 *
                 *  登录成功之后,进行页面跳转:
                 *
                 *  Intent intent = new Intent(this, MainActivity.class);
                 *  startActivity(intent);
                 *  finish();//销毁此Activity
                 */
                case R.id.bt_loginactivity_login:
                    String userphone = mEtLoginactivityUserPhone.getText().toString().trim();
                    if (!TextUtils.isEmpty(userphone)) {
                        ArrayList<User> data = mDBOpenHelper.getAllData();
                        boolean match = false;
                        for (int i = 0; i < data.size(); i++) {
                            User user = data.get(i);
                            if (userphone.equals(user.getUserphone())||userphone.equals("3")) {
                                match = true;
                                break;
                            } else {
                                match = false;
                            }
                        }
                        if (match) {
                            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(this, MainActivity.class);
                            startActivity(intent);
                            finish();//销毁此Activity
                        } else {
                            Toast.makeText(this, "手机号不正确,请重新输入", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "请输入你的手机号", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    }
    复制代码

    Code.java

    复制代码
    package com.example.helloworld;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    
    import java.util.Random;
    
    
    public class Code {
        /**
         * 随机数数组
         * 去除了易混淆的 数字 0 和 字母 o O
         *                数字 1 和 字母 i I l L
         *                数字 6 和 字母 b
         *                数字 9 和 字母 q
         *                字母 c C 和 G
         *                字母 t (经常和随机线混在一起看不清)
         */
        private static final char[] CHARS = {
                '2', '3', '4', '5',  '7', '8',
                'a',  'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm',
                'n', 'p',  'r', 's',  'u', 'v', 'w', 'x', 'y', 'z',
                'A', 'B',  'D', 'E', 'F',  'H',  'J', 'K', 'M',
                'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        };
    
        private static Code bmpCode;
    
        public static Code getInstance() {
            if(bmpCode == null)
                bmpCode = new Code();
            return bmpCode;
        }
    
        //default settings
        //验证码默认随机数的个数
        private static final int DEFAULT_CODE_LENGTH = 4;
        //默认字体大小
        private static final int DEFAULT_FONT_SIZE = 25;
        //默认线条的条数
        private static final int DEFAULT_LINE_NUMBER = 5;
        //padding值
        private static final int BASE_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;
        //验证码的默认宽高
        private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40;
    
        //settings decided by the layout xml
        //canvas width and height
        private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
    
        //random word space and pading_top
        private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,
                base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;
    
        //number of chars, lines; font size
        private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;
    
        //variables
        private String code;
        private int padding_left, padding_top;
        private Random random = new Random();
        //验证码图片
        public Bitmap createBitmap() {
            padding_left = 0;
    
            Bitmap bp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(bp);
    
            code = createCode();
    
            c.drawColor(Color.WHITE);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setTextSize(font_size);
            //画验证码
            for (int i = 0; i < code.length(); i++) {
                randomTextStyle(paint);
                randomPadding();
                c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);
            }
            //画线条
            for (int i = 0; i < line_number; i++) {
                drawLine(c, paint);
            }
    
            //        c.save( Canvas.ALL_SAVE_FLAG );//保存
            c.save();//保存
            c.restore();//
            return bp;
        }
    
        public String getCode() {
            return code;
        }
    
        //生成验证码
        private String createCode() {
            StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < codeLength; i++) {
                buffer.append(CHARS[random.nextInt(CHARS.length)]);
            }
            return buffer.toString();
        }
        //画干扰线
        private void drawLine(Canvas canvas, Paint paint) {
            int color = randomColor();
            int startX = random.nextInt(width);
            int startY = random.nextInt(height);
            int stopX = random.nextInt(width);
            int stopY = random.nextInt(height);
            paint.setStrokeWidth(1);
            paint.setColor(color);
            canvas.drawLine(startX, startY, stopX, stopY, paint);
        }
        //生成随机颜色
        private int randomColor() {
            return randomColor(1);
        }
    
        private int randomColor(int rate) {
            int red = random.nextInt(256) / rate;
            int green = random.nextInt(256) / rate;
            int blue = random.nextInt(256) / rate;
            return Color.rgb(red, green, blue);
        }
        //随机生成文字样式,颜色,粗细,倾斜度
        private void randomTextStyle(Paint paint) {
            int color = randomColor();
            paint.setColor(color);
            paint.setFakeBoldText(random.nextBoolean());  //true为粗体,false为非粗体
            float skewX = random.nextInt(11) / 10;
            skewX = random.nextBoolean() ? skewX : -skewX;
            paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
            //paint.setUnderlineText(true); //true为下划线,false为非下划线
            //paint.setStrikeThruText(true); //true为删除线,false为非删除线
        }
        //随机生成padding值
        private void randomPadding() {
            padding_left += base_padding_left + random.nextInt(range_padding_left);
            padding_top = base_padding_top + random.nextInt(range_padding_top);
        }
    }
    复制代码
  • 相关阅读:
    Java IO学习3:字节字符转换流
    Java IO学习8:System类对IO的支持
    设计模式(一)单例模式
    复制excel下拉框的数值
    iis7.5+win2008 出现 HTTP Error 503. The service is unavailable.
    php显示当前数据库名称
    解决secureCRT显示中文为乱码
    Microsoft OLE DB Provider for ODBC Drivers 错误 '80040e23 ' [Microsoft][ODBC SQL
    jquery textSlider 文字滚动
    donetcms与Discuz整合的webconfig设置
  • 原文地址:https://www.cnblogs.com/zhaoyuxiao000/p/14908957.html
Copyright © 2020-2023  润新知