欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android studio 入门实战——注册登录+验证功能的实现

程序员文章站 2022-06-18 13:57:57
文章目录1 注册2 登录3 验证码实现4 数据库1 注册注册页面展示:注册页面代码

1 注册

  • 注册页面展示:
    Android studio 入门实战——注册登录+验证功能的实现

  • 注册页面代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical"
    tools:context=".RegisterActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="253dp"
        android:scaleType="fitXY"
        android:src="@mipmap/a002"/>
    <ImageView
        android:id="@+id/iv_registeractivity_back"
        android:layout_width="17dp"
        android:layout_height="30dp"
        android:background="@drawable/ic_left_back"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:clickable="true"
        android:onClick="onClick"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="210dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginLeft="18dp"
            android:layout_marginRight="18dp"
            android:background="@drawable/shape_login_form"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingLeft="43dp"
            android:paddingRight="31dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@mipmap/account" />

                <EditText
                    android:id="@+id/et_registeractivity_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:background="@null"
                    android:hint="@string/account_hint"
                    android:textColor="#000000"
                    android:textColorHint="#bcbcbc"
                    android:textSize="18sp" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="23dp"
                android:layout_marginBottom="23dp"
                android:background="@color/divider" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@mipmap/pwd" />

                <EditText
                    android:id="@+id/et_registeractivity_password1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:background="@null"
                    android:hint="@string/pwd_hint1"
                    android:inputType="textPassword"
                    android:textColor="@color/black"
                    android:textColorHint="#bcbcbc"
                    android:textSize="18sp" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="23dp"
                android:layout_marginBottom="23dp"
                android:background="@color/divider" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@mipmap/pwd" />

                <EditText
                    android:id="@+id/et_registeractivity_password2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:background="@null"
                    android:hint="@string/pwd_hint2"
                    android:inputType="textPassword"
                    android:textColor="@color/black"
                    android:textColorHint="#bcbcbc"
                    android:textSize="18sp" />
            </LinearLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="23dp"
                android:layout_marginBottom="23dp"
                android:background="@color/divider" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginRight="15dp"
                android:orientation="horizontal">
                <!-- 第四个文本编辑框  输入验证码 -->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/et_registeractivity_phoneCodes"
                        android:layout_width="196dp"
                        android:layout_height="50dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_weight="3"
                        android:hint="@string/code_hint"
                        android:textColor="#000000"
                        android:textColorHint="#bcbcbc" />

                    <ImageView
                        android:id="@+id/iv_registeractivity_showCode"
                        android:layout_width="95dp"
                        android:layout_height="63dp"
                        android:layout_marginTop="-10dp"
                        android:layout_weight="1.5"
                        android:clickable="true"
                        android:onClick="onClick" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

        <Button
            android:id="@+id/bt_registeractivity_register"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="40dp"
            android:layout_marginRight="18dp"
            android:background="@drawable/shape_big_register_btn"
            android:text="@string/register"
            android:textColor="@color/white"
            android:textSize="24sp" />
    </LinearLayout>

</RelativeLayout>
  • 注册按钮
    Android studio 入门实战——注册登录+验证功能的实现

这里注册按钮的格式是通过xml代码设计的,代码文件放在drawable文件夹下:

shape_big_register_btn.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="9dp" />
    <solid android:color="#EA9F44" />
</shape>
  • 注册框以及登录框的设计:
    Android studio 入门实战——注册登录+验证功能的实现

以下xml文件也是在drawable文件夹下新建

shape_login_form.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 边 -->
    <item>
        <shape android:shape="rectangle">
            <padding
                android:bottom="2dp"
                android:left="1.5dp"
                android:right="2dp"
                android:top="1.5dp" />
            <solid android:color="#F2F2F2" />
            <corners android:radius="8dp" />
        </shape>
    </item>
    <!-- 中心背景 -->
    <item>
        <shape
            android:shape="rectangle"
            android:useLevel="false">
            <!-- 实心 -->
            <solid android:color="#ffffff" />
            <corners android:radius="10dp" />
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />
        </shape>
    </item>
</layer-list>
  • 注册功能实现代码
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.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private String realCode;
    private DBOpenHelper mDBOpenHelper;
    private Button mBtRegisteractivityRegister;
    private ImageView mIvRegisteractivityBack;
    private EditText mEtRegisteractivityUsername;
    private EditText mEtRegisteractivityPassword1;
    private EditText mEtRegisteractivityPassword2;
    private EditText mEtRegisteractivityPhonecodes;
    private ImageView mIvRegisteractivityShowcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();

        mDBOpenHelper = new DBOpenHelper(this);

        //将验证码用图片的形式显示出来
        mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
        realCode = Code.getInstance().getCode().toLowerCase();
    }

    private void initView(){
        mBtRegisteractivityRegister = findViewById(R.id.bt_registeractivity_register);
        mIvRegisteractivityBack = findViewById(R.id.iv_registeractivity_back);
        mEtRegisteractivityUsername = findViewById(R.id.et_registeractivity_username);
        mEtRegisteractivityPassword1 = findViewById(R.id.et_registeractivity_password1);
        mEtRegisteractivityPassword2 = findViewById(R.id.et_registeractivity_password2);
        mEtRegisteractivityPhonecodes = findViewById(R.id.et_registeractivity_phoneCodes);
        mIvRegisteractivityShowcode = findViewById(R.id.iv_registeractivity_showCode);

        /**
         * 注册页面能点击的就三个地方
         * top处返回箭头、刷新验证码图片、注册按钮
         */
        mIvRegisteractivityBack.setOnClickListener(this);
        mIvRegisteractivityShowcode.setOnClickListener(this);
        mBtRegisteractivityRegister.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_registeractivity_back: //返回登录页面
                Intent intent1 = new Intent(this, LoginActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.iv_registeractivity_showCode:    //改变随机验证码的生成
                mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                break;
            case R.id.bt_registeractivity_register:    //注册按钮
                //获取用户输入的用户名、密码、验证码
                String username = mEtRegisteractivityUsername.getText().toString().trim();
                String password1 = mEtRegisteractivityPassword1.getText().toString().trim();
                String password2 = mEtRegisteractivityPassword2.getText().toString().trim();
                String phoneCode = mEtRegisteractivityPhonecodes.getText().toString().toLowerCase();
                //注册验证
                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password1)&& !TextUtils.isEmpty(password2) && !TextUtils.isEmpty(phoneCode) ) {
                    if (phoneCode.equals(realCode)) {
                        if (!(password1==password2)) {
                            //将用户名和密码加入到数据库中
                            mDBOpenHelper.add(username, password2);
                            Intent intent2 = new Intent(this, MainActivity.class);
                            startActivity(intent2);
                            finish();
                            Toast.makeText(this, "验证通过,注册成功", Toast.LENGTH_SHORT).show();
                        }else
                        {
                            Toast.makeText(this, "密码输入不一致,请重新输入", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "验证码错误,注册失败", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "未完善信息,注册失败", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}


2 登录

  • 登录界面展示:
    Android studio 入门实战——注册登录+验证功能的实现
  • 登录页面代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".RegisterActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="253dp"
        android:scaleType="fitXY"
        android:src="@mipmap/a001" />

    <Button
        android:id="@+id/tv_loginactivity_register"
        android:layout_width="55dp"
        android:layout_height="70dp"
        android:layout_marginLeft="280dp"
        android:background="@android:color/transparent"
        android:text="@string/register"
        android:textColor="#85FFFFFF"
        android:textColorHint="#bcbcbc"
        android:textSize="22sp"
        android:onClick="onClick"/>
    <ImageView
        android:layout_width="17dp"
        android:layout_height="30dp"
        android:layout_marginLeft="335dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/right"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="240dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="260dp"
            android:layout_marginLeft="18dp"
            android:layout_marginRight="18dp"
            android:background="@drawable/shape_login_form"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingLeft="43dp"
            android:paddingRight="31dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@mipmap/account" />

                <EditText
                    android:id="@+id/et_loginactivity_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:background="@null"
                    android:hint="@string/account_hint"
                    android:textColor="#000000"
                    android:textColorHint="#bcbcbc"
                    android:textSize="18sp" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="23dp"
                android:layout_marginBottom="23dp"
                android:background="#e8e7e7" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:src="@mipmap/pwd" />

                <EditText
                    android:id="@+id/et_loginactivity_password"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:background="@null"
                    android:hint="@string/pwd_hint1"
                    android:inputType="textPassword"
                    android:textColor="#000000"
                    android:textColorHint="#bcbcbc"
                    android:textSize="18sp" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="23dp"
                android:layout_marginBottom="23dp"
                android:background="@color/divider" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginRight="15dp"
                android:orientation="horizontal">
                <!-- 第四个文本编辑框  输入验证码 -->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/et_loginactivity_phoneCodes"
                        android:layout_width="196dp"
                        android:layout_height="50dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_weight="3"
                        android:hint="@string/code_hint"
                        android:textColor="#000000"
                        android:textColorHint="#bcbcbc" />

                    <ImageView
                        android:id="@+id/iv_loginactivity_showCode"
                        android:layout_width="95dp"
                        android:layout_height="63dp"
                        android:layout_marginTop="-10dp"
                        android:layout_weight="1.5"
                        android:clickable="true"
                        android:onClick="onClick" />
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>

        <Button
            android:id="@+id/bt_loginactivity_login"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="50dp"
            android:layout_marginRight="18dp"
            android:background="@drawable/shape_big_login_btn"
            android:text="@string/login"
            android:textColor="#ffffff"
            android:textSize="24sp" />
    </LinearLayout>
</RelativeLayout>
  • 登录按钮
    Android studio 入门实战——注册登录+验证功能的实现

shape_big_login_btn.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="9dp" />
    <solid android:color="#A440B5" />
</shape>
  • 登录功能实现代码:

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private String realCode;
    private DBOpenHelper mDBOpenHelper;
    private TextView mTvLoginactivityRegister;
    private EditText mEtLoginactivityUsername;
    private EditText mEtLoginactivityPassword;
    private Button mBtLoginactivityLogin;
    private EditText mEtloginactivityPhonecodes;
    private ImageView mIvloginactivityShowcode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();

        mDBOpenHelper = new DBOpenHelper(this);

        //将验证码用图片的形式显示出来
        mIvloginactivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
        realCode = Code.getInstance().getCode().toLowerCase();


    }

    private void initView() {
        // 初始化控件
        mBtLoginactivityLogin = findViewById(R.id.bt_loginactivity_login);
        mTvLoginactivityRegister = findViewById(R.id.tv_loginactivity_register);
        mEtLoginactivityUsername = findViewById(R.id.et_loginactivity_username);
        mEtLoginactivityPassword = findViewById(R.id.et_loginactivity_password);
        mEtloginactivityPhonecodes = findViewById(R.id.et_loginactivity_phoneCodes);
        mIvloginactivityShowcode = findViewById(R.id.iv_loginactivity_showCode);

        // 设置点击事件监听器
        mBtLoginactivityLogin.setOnClickListener(this);
        mIvloginactivityShowcode.setOnClickListener(this);

    }

    public void onClick(View view) {
        switch (view.getId()) {
            // 跳转到注册界面
            case R.id.tv_loginactivity_register:
                startActivity(new Intent(this, RegisterActivity.class));
                finish();
                break;

            case R.id.iv_loginactivity_showCode:    //改变随机验证码的生成
                mIvloginactivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                break;
            case R.id.bt_loginactivity_login:
                String name = mEtLoginactivityUsername.getText().toString().trim();
                String password = mEtLoginactivityPassword.getText().toString().trim();
                String phoneCode = mEtloginactivityPhonecodes.getText().toString().toLowerCase();

                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)&&!TextUtils.isEmpty(phoneCode)) {
                    if (phoneCode.equals(realCode)) {
                        ArrayList<User> data = mDBOpenHelper.getAllData();
                        boolean match = false;
                        for (int i = 0; i < data.size(); i++) {
                            User user = data.get(i);
                            if (name.equals(user.getName()) && password.equals(user.getPassword())/*&&phoneCode.equals(realCode)*/) {
                                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();
                    }
                } else {
                    Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
                }break;


        }


    }
}





3 验证码实现

  • 验证码生成
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);
    }
}


4 数据库

  • 相关代码:
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;

public class DBOpenHelper extends SQLiteOpenHelper {
    private SQLiteDatabase db;

    public DBOpenHelper(Context context){
        super(context,"db_map",null,1);
        db = getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE IF NOT EXISTS user(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "name TEXT," +
                "password TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS user");
        onCreate(db);
    }
    
    public void add(String name,String password){
        db.execSQL("INSERT INTO user (name,password) VALUES(?,?)",new Object[]{name,password});
    }
    public void delete(String name,String password){
        db.execSQL("DELETE FROM user WHERE name = AND password ="+name+password);
    }
    public void updata(String password){
        db.execSQL("UPDATE user SET password = ?",new Object[]{password});
    }

  
    public ArrayList<User> getAllData(){

        ArrayList<User> list = new ArrayList<User>();
        Cursor cursor = db.query("user",null,null,null,null,null,"name DESC");
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            list.add(new User(name,password));
        }
        return list;
    }
}

public class User {
    private String name;            //用户名
    private String password;        //密码
    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}


本文地址:https://blog.csdn.net/dawn_1108/article/details/108123041