Linux 安装python 3.8(Linux 的版本为 Centos 7)

利用sqlite实现简易登录注册以及修改密码功能。

页面设计
登录页面activity_login.xml

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

    <RelativeLayout
        android:id="@+id/rtLytTop"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="30dp"
        >



    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rtLytMiddle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/rtLytTop"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="1dp"
        android:layout_marginRight="30dp"
        >

        <EditText
            android:id="@+id/edt_UserName"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/editext_shape"
            android:hint="请输入用户名"
            android:inputType="text">

        </EditText>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/rtLyMiddle"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/rtLytMiddle"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="30dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/edt_Password"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/editext_shape"
            android:hint="请输入密码"
            android:inputType="textPassword">

        </EditText>

    </LinearLayout>
    <Button
        android:id="@+id/btn_Login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/rtLyMiddle"
        android:layout_margin="70dp"
        android:text="登录"
        android:background="@drawable/button_shape"
        android:textColor="#ffffff"
        android:textSize="24sp">

    </Button>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/btn_Forget"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#0BD1EA"
            android:paddingLeft="10dp"
            android:paddingBottom="10dp"
            android:text="忘记密码?">

        </Button>

        <Button
            android:id="@+id/btn_Register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="#0BD1EA"
            android:paddingRight="10dp"
            android:paddingBottom="10dp"
            android:text="新用户">

        </Button>
    </RelativeLayout>


</RelativeLayout>

界面截图
在这里插入图片描述
注册界面
activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0BD1EA"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户注册"
        android:textSize="30sp"
        android:gravity="center">

    </TextView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <TextView
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="用  户  名:"
            android:textSize="24sp"></TextView>


        <EditText
            android:id="@+id/edt_UserName"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:hint="请输入用户名"
            android:inputType="text"></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <TextView
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="密        码:"
            android:textSize="24sp"></TextView>

        <EditText
            android:id="@+id/edt_Password"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:hint="请输入密码(6-12位)"
            android:inputType="textPassword"
            android:maxLength="12"></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <TextView
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="确认密码:"
            android:textSize="24sp"></TextView>

        <EditText
            android:id="@+id/edt_Password2"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:hint="请确认密码(6-12位)"
            android:inputType="textPassword"></EditText>

    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <TextView
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="手  机  号:"
            android:textSize="24sp"></TextView>

        <EditText
            android:id="@+id/edt_Phone"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:hint="请输入手机号"
            android:inputType="number"
            android:maxLength="12"></EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <TextView
            android:id="@+id/Sex"
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="性        别:"
            android:textSize="24sp"></TextView>

        <RadioGroup
            android:id="@+id/rdg_Sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_Man"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男" />

            <RadioButton
                android:id="@+id/rb_Woman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="60dp"
                android:text="女" />

        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp">

        <TextView

            android:layout_width="120dp"
            android:layout_height="50dp"
            android:text="出  生  地:"
            android:textSize="24sp" />

        <TextView

            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="省份:"
            android:textSize="18dp" />

        <Spinner
            android:id="@+id/sp_Provinces"
            android:layout_width="100dp"
            android:layout_height="wrap_content"></Spinner>

        <TextView

            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="城市:"
            android:textSize="18dp" />

        <Spinner
            android:id="@+id/sp_Cities"
            android:layout_width="100dp"
            android:layout_height="wrap_content"></Spinner>

    </LinearLayout>
    <Button
        android:id="@+id/btn_Register"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="70dp"
        android:text="确认注册"
        android:background="@drawable/button_shape"
        android:textColor="#ffffff"
        android:textSize="24sp"></Button>


</LinearLayout>

界面截图
在这里插入图片描述
修改密码界面
activity_forget_password.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0BD1EA"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="修改密码"
            android:textSize="30sp"
            android:gravity="center"></TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="132dp"
            android:layout_height="wrap_content"
            android:text="账            号:"
            android:textSize="24sp"
            android:layout_marginLeft="10dp">

        </TextView>
        <EditText
            android:id="@+id/edt_UserName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp">

        </EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="132dp"
            android:layout_height="wrap_content"
            android:text="手    机    号:"
            android:textSize="24sp"
            android:layout_marginLeft="10dp">

        </TextView>
        <EditText
            android:id="@+id/edt_Phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入注册的手机号"
            android:inputType="number"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp">

        </EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="132dp"
            android:layout_height="wrap_content"
            android:text="新    密    码:"

            android:textSize="24sp"
            android:layout_marginLeft="10dp">

        </TextView>
        <EditText
            android:id="@+id/edt_NewPassWord"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入新密码"
            android:inputType="textPassword"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp">

        </EditText>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="132dp"
            android:layout_height="wrap_content"
            android:text="确认新密码:"
            android:textSize="24sp"
            android:layout_marginLeft="10dp">

        </TextView>
        <EditText
            android:id="@+id/edt_NewPassWord2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请再次输入新密码"
            android:inputType="textPassword"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp">

        </EditText>
    </LinearLayout>

    <Button
        android:id="@+id/btn_Confirm"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="70dp"
        android:text="确认修改"
        android:background="@drawable/button_shape"
        android:textColor="#ffffff"
        android:textSize="24sp">

    </Button>


</LinearLayout>

界面截图
在这里插入图片描述
这几个界面的按钮都进行的圆角的设计,可参考
button_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="#5668CA"></solid>
    <corners
        android:radius="25dp">

    </corners>


</shape>

这些背景颜色什么的可以根据自己需要进行设计哈,然后将其放在drawable文件夹下,需要用到的时候直接调用就可以了。尽量规范命名哈。
功能实现
登录功能实现,Login.java

/**
 * 账号密码登录
 */

public class Login extends AppCompatActivity {
    private EditText edt_Username, edt_PassWord;
    private Button btn_Login, btn_Forget, btn_Register;
    boolean isFlag = false;

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

        edt_Username = this.findViewById(R.id.edt_UserName);
        edt_PassWord = this.findViewById(R.id.edt_Password);
        btn_Login = this.findViewById(R.id.btn_Login);
        btn_Forget = this.findViewById(R.id.btn_Forget);
        btn_Register = this.findViewById(R.id.btn_Register);
        //用户名
        edt_Username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    //当点击用户名输入框时,清空密码框
                    edt_PassWord.setText("");
                } else {
                    String username = edt_Username.getText().toString();
                    if (username.length() < 4) {
                        Toast.makeText(Login.this, "用户名长度必须大于4,请重新输入", Toast.LENGTH_SHORT).show();
                        edt_Username.setText("");

                    }
                }
            }
        });
        //密码框
        edt_PassWord.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {

                } else {
                    String password = edt_PassWord.getText().toString();
                    if (password.length() < 6 || password.length() > 12) {
                        Toast.makeText(Login.this, "密码长度必须为6-12位,请重新输入", Toast.LENGTH_LONG).show();
                        edt_PassWord.setText("");

                    }
                }
            }
        });
        //打开数据库
        SQLiteDatabase database = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
        //若不存在user表,则创建user表
        String createSQL = "create table IF NOT EXISTS user(username text,password text,password2 text,phone text)";
        database.execSQL(createSQL);//执行创表语句
        //登录事件监听
        btn_Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isFlag = false;
                String username = edt_Username.getText().toString();
                String password = edt_PassWord.getText().toString();
                //判断输入是否为空,若为空,给出提示
                if (username.equals("") || password.equals("")) {
                    Toast.makeText(Login.this, "请输入账号或密码!", Toast.LENGTH_SHORT).show();
                    isFlag = true;
                }
                //查询账号密码,若数据库表为空,提示用户注册

                Cursor cursor = database.query("user", new String[]{"username,password"}, null, null, null, null, null);
                if (cursor.getCount() == 0) {
                    Toast.makeText(Login.this, "请先注册账号!", Toast.LENGTH_SHORT).show();
                isFlag=true;
                }
                //若数据库表不为空,查看用户输入的账号密码是否与数据库表中的相匹配,若匹配,登录成功,跳转到主界面
                // 否则无法登录,给出账号密码错误提示
                else {
                    while (cursor.moveToNext()) {

                        if (username.equals(cursor.getString(cursor.getColumnIndex("username"))) && password.equals(cursor.getString(cursor.getColumnIndex("password")))) {
                            Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(Login.this, MainActivity.class);
                            startActivity(intent);
                            isFlag = true;
                            break;

                        }
                    }
                    if (isFlag == false) {
                        Toast.makeText(Login.this, "账号或密码错误,请重新输入", Toast.LENGTH_SHORT).show();
                    }
                }


            }


        });
        //修改密码监听事件,点击忘记密码,跳转到修改密码界面
        btn_Forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login.this, Forget_Password.class);
                startActivity(intent);

            }
        });
        //注册监听事件,点击新用户,跳转到注册界面
        btn_Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Login.this, Register.class);
                startActivity(intent);
            }
        });
    }
}

利用数据库查询账号是都存在,以及账号密码是否匹配验证,否则是登录不成功的,若没有账号就先注册账号。
注册账号功能
Register.java

/**
 * 用户注册
 */
public class Register extends AppCompatActivity {

    private Spinner sp_Provinces;
    private Spinner sp_Cities;
    private RadioGroup rdg_Sex;
    private RadioButton rb_Man, rb_Woman;
    private Button btn_Register;
    private EditText edt_UserName, edt_Password, edt_Password2, edt_Phone;
    private boolean isFlag = true;

    @Override

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

        //用户名及密码
        edt_UserName = this.findViewById(R.id.edt_UserName);
        edt_Password = this.findViewById(R.id.edt_Password);
        edt_Password2 = this.findViewById(R.id.edt_Password2);

        //手机号
        edt_Phone = this.findViewById(R.id.edt_Phone);

        //出生地
        sp_Provinces = this.findViewById(R.id.sp_Provinces);
        sp_Cities = this.findViewById(R.id.sp_Cities);

        //性别选择
        rdg_Sex = this.findViewById(R.id.rdg_Sex);
        rb_Man = this.findViewById(R.id.rb_Man);
        rb_Woman = this.findViewById(R.id.rb_Woman);

        //确认注册按钮
        btn_Register = this.findViewById(R.id.btn_Register);

        //用户名输入框失焦处理
        edt_UserName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    isFlag = true;
                } else {
                    isFlag = true;
                    String username = edt_UserName.getText().toString();
                    if (username.length() < 4) {
                        Toast.makeText(Register.this, "用户名长度必须大于4,请重新输入", Toast.LENGTH_SHORT).show();
                        edt_UserName.setText("");
                    }
                }
            }
        });
        //密码输入框失焦处理
        edt_Password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    isFlag=true;
                } else {
                    isFlag = true;
                    String password = edt_Password.getText().toString();
                    if (password.length() < 6 || password.length() > 12) {
                        Toast.makeText(Register.this, "密码长度必须为6-12位,请重新输入", Toast.LENGTH_LONG).show();
                        edt_Password.setText("");
                    }
                }
            }
        });
        //确认密码输入框失焦处理
        edt_Password2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    isFlag=true;
                } else {
                    isFlag = true;
                    String password = edt_Password.getText().toString();
                    String password2 = edt_Password2.getText().toString();

                    if (!(password.equals(password2))) {
                        Toast.makeText(Register.this, "两次密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
                        edt_Password2.setText("");

                    } else if (password2.length() < 6 || password2.length() > 12) {
                        Toast.makeText(Register.this, "密码长度必须为6-12位", Toast.LENGTH_LONG).show();
                        edt_Password2.setText("");
                    }

                }
            }

        });
        //手机号
        edt_Phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    isFlag=true;
                } else {
                    isFlag = true;
                    String phone = edt_Phone.getText().toString();
                    if (!(phone.length() == 11)) {
                        Toast.makeText(Register.this, "请输入11位手机号", Toast.LENGTH_SHORT).show();
                        edt_Phone.setText("");
                    }
                }
            }
        });
        //性别处理
        rdg_Sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_Man:
                        Toast.makeText(Register.this, "男", Toast.LENGTH_LONG).show();
                        break;
                    case R.id.rb_Woman:
                        Toast.makeText(Register.this, "女", Toast.LENGTH_LONG).show();
                        break;
                }
            }
        });
        //出生地处理
        ArrayAdapter adapter = ArrayAdapter.createFromResource(Register.this, R.array.provinces, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        sp_Provinces.setAdapter(adapter);
        sp_Provinces.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override//省市联动
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position == 0) {
                    ArrayAdapter cityAdapter = ArrayAdapter.createFromResource(Register.this, R.array.yunnan, android.R.layout.simple_spinner_item);
                    sp_Cities.setAdapter(cityAdapter);
                } else if (position == 1) {
                    ArrayAdapter cityAdapter = ArrayAdapter.createFromResource(Register.this, R.array.shanxi, android.R.layout.simple_spinner_item);
                    sp_Cities.setAdapter(cityAdapter);
                } else if (position == 2) {
                    ArrayAdapter cityAdapter = ArrayAdapter.createFromResource(Register.this, R.array.fj, android.R.layout.simple_spinner_item);

                    sp_Cities.setAdapter(cityAdapter);
                } else if (position == 3) {
                    ArrayAdapter cityAdapter = ArrayAdapter.createFromResource(Register.this, R.array.guangxi, android.R.layout.simple_spinner_item);

                    sp_Cities.setAdapter(cityAdapter);
                } else if (position == 4) {
                    ArrayAdapter cityAdapter = ArrayAdapter.createFromResource(Register.this, R.array.guangdong, android.R.layout.simple_spinner_item);

                    sp_Cities.setAdapter(cityAdapter);
                } else if (position == 5) {
                    ArrayAdapter cityAdapter = ArrayAdapter.createFromResource(Register.this, R.array.guizhou, android.R.layout.simple_spinner_item);
                    sp_Cities.setAdapter(cityAdapter);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //打开数据库或创建数据库
        SQLiteDatabase database = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
        String createSQL = "create table IF NOT EXISTS user(username text,password text,password2 text,phone text)";
        database.execSQL(createSQL);
        //确认注册处理
        btn_Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入信息
                String username = edt_UserName.getText().toString();
                String password = edt_Password.getText().toString();
                String password2 = edt_Password2.getText().toString();
                String phone = edt_Phone.getText().toString();
                //判断用户输入是否为空,若有一个输入框为空,则给出提示信息

                if (username.equals("") || password.equals("") || password2.equals("") || phone.equals("")) {
                    Toast.makeText(Register.this, "请输入完整信息", Toast.LENGTH_SHORT).show();
                    isFlag = false;


                }
                //判断用户是否存在,若已有此账号,则不允许重复注册,否则允许注册
                Cursor cursor = database.query("user", new String[]{"username"}, null, null, null, null, null);
                while (cursor.moveToNext()) {
                    if (username.equals(cursor.getString(cursor.getColumnIndex("username")))) {
                        Toast.makeText(Register.this, "该账户已存在", Toast.LENGTH_SHORT).show();
                        isFlag = false;//
                    }
                }

                 if(!password.equals(password2)) {

                    Toast.makeText(Register.this, "两次密码不一致", Toast.LENGTH_SHORT).show();

                    //若允许注册,则将用户输入的信息,插入到数据库表(user)中,插入成功跳转到登录界面
                 }else if (!(phone.length() == 11)){
                     Toast.makeText(Register.this, "请输入11位手机号!", Toast.LENGTH_SHORT).show();
                 }
                 else if (isFlag ) {
                    ContentValues values = new ContentValues();
                    //将数据放入values中
                    values.put("username", username);
                    values.put("password", password);
                    values.put("password2", password2);
                    values.put("phone", phone);
                    //用insert()方法将values中的数据插入到user表中
                    database.insert("user", null, values);
                    Toast.makeText(Register.this, "注册成功,请登录!", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Register.this, Login.class);
                    startActivity(intent);
                    database.close();
                }


            }
        });
    }


}

用户注册的账号密码保存在数据库中,手机号用作验证功能,不过选择出生地的时候还没有加入数据库的哈,数据库中没有出生地属性,大家可以自行加入。

最后为忘记密码功能
Forget_Password.java

/**
 * 修改密码
 */
public class Forget_Password extends AppCompatActivity {
    private EditText edt_UserName, edt_Phone, edt_NewPassWord, edt_NewPassWord2;
    private Button btn_Confirm;
    private boolean isFlag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        //获取实例对象
        edt_UserName = this.findViewById(R.id.edt_UserName);
        edt_Phone = this.findViewById(R.id.edt_Phone);
        edt_NewPassWord = this.findViewById(R.id.edt_NewPassWord);
        edt_NewPassWord2 = this.findViewById(R.id.edt_NewPassWord2);
        btn_Confirm = this.findViewById(R.id.btn_Confirm);

        //用户名
        edt_UserName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {

                } else {
                    String username = edt_UserName.getText().toString();
                    if (username.length() < 4) {
                        Toast.makeText(Forget_Password.this, "用户名长度必须大于4,请重新输入", Toast.LENGTH_SHORT).show();
                        edt_UserName.setText("");
                    }
                }
            }
        });

        //手机号,用作用户修改密码验证条件
        edt_Phone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {


                } else {
                    String phone = edt_Phone.getText().toString();
                    if (!(phone.length() == 11)) {
                        Toast.makeText(Forget_Password.this, "请输入11位手机号", Toast.LENGTH_SHORT).show();
                        edt_Phone.setText("");
                    }


                }
            }
        });

        //新密码
        edt_NewPassWord.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {

                } else {
                    String newPassword = edt_NewPassWord.getText().toString();
                    if (newPassword.length() < 6 || newPassword.length() > 12) {
                        Toast.makeText(Forget_Password.this, "密码长度必须为6-12位,请重新输入", Toast.LENGTH_LONG).show();
                        edt_NewPassWord.setText("");
                    }
                }
            }
        });
        //确认新密码
        edt_NewPassWord2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {

                } else {
                    String newPassword = edt_NewPassWord.getText().toString();
                    String newPassword2 = edt_NewPassWord2.getText().toString();

                    if (!(newPassword.equals(newPassword2))) {
                        Toast.makeText(Forget_Password.this, "两次密码不一致,请重新输入", Toast.LENGTH_SHORT).show();
                        edt_NewPassWord2.setText("");

                    } else if (newPassword2.length() < 6 || newPassword2.length() > 12) {
                        Toast.makeText(Forget_Password.this, "密码长度必须为6-12位", Toast.LENGTH_LONG).show();
                        edt_NewPassWord2.setText("");
                    }

                }
            }
        });
        //确认修改密码,若用户名与此手机号匹配,则可以修改密码,否则不允许修改密码
        SQLiteDatabase database = openOrCreateDatabase("user.db", MODE_PRIVATE, null);
        btn_Confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username = edt_UserName.getText().toString();
                String password = edt_NewPassWord.getText().toString();
                String password2 = edt_NewPassWord2.getText().toString();
                String phone = edt_Phone.getText().toString();
                //判断用户输入是否为空,若有一个输入框为空,则给出提示信息
                if (username.equals("") || password.equals("") || password2.equals("") || phone.equals("")) {
                    Toast.makeText(Forget_Password.this, "请输入完整信息", Toast.LENGTH_SHORT).show();

                }else if (!(password.equals(password2))){
                    Toast.makeText(Forget_Password.this, "两次密码不一致", Toast.LENGTH_SHORT).show();

                }else {

                Cursor cursor = database.query("user", new String[]{"username,phone"}, null, null, null, null, null);
                while (cursor.moveToNext()) {
                    //判断用户名和手机号是否匹配
                    //首先利用查询语句,查询username和phone,若手机号和用户名匹配,则允许修改密码。
                    if ((username.equals(cursor.getString(cursor.getColumnIndex("username")))
                            && phone.equals(cursor.getString(cursor.getColumnIndex("phone"))))) {
                        ContentValues values = new ContentValues();
                        //将数据放入values中
                        String Username = edt_UserName.getText().toString();
                        values.put("password", password);
                        values.put("password2", password2);
                        //用update()方法将values中的数据更新对应的用户名密码
                        database.update("user", values, "username=?", new String[]{username});
                        Toast.makeText(Forget_Password.this, "修改密码成功,请登录!", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Forget_Password.this, Login.class);
                        isFlag = true;
                        startActivity(intent);
                        database.close();
                        break;

                    }
                    isFlag = false;
                }
                }
                if (isFlag == false) {
                    Toast.makeText(Forget_Password.this, "用户名与手机号不匹配,修改失败", Toast.LENGTH_SHORT).show();
                }


            }
        });
    }
}

手机号为用户注册时的手机号,暂时用手机号作为验证功能,用户的账号和手机号与数据库的匹配就允许修改密码。
地名资源文件
放在res文件夹的values的string.xml文件
在这里插入图片描述

<resources>
    <string name="app_name">Words</string>
    <string-array name="provinces">
        <item>云南省</item>
        <item>山西省</item>
        <item>福建省</item>
        <item>广西壮族自治区</item>
        <item>广东省</item>
        <item>贵州省</item>
    </string-array>

    <string-array name="yunnan">


        <item>昆明市</item>
        <item>曲靖市</item>
        <item>玉溪市</item>
        <item>昭通市</item>
        <item>保山市</item>
        <item>丽江市</item>

    </string-array>

    <string-array name="guangxi">

        <item>南宁市</item>
        <item>桂林市</item>
        <item>柳州市</item>
        <item>北海市</item>
        <item>钦州市</item>
        <item>玉林市</item>
        <item>百色市</item>
        <item>河池市</item>
        <item>防城港市</item>
    </string-array>
    <string-array name="shanxi">

        <item>大同市</item>
        <item>临汾市</item>
        <item>太原市</item>
        <item>阳泉市</item>
        <item>长治市</item>
        <item>晋城市</item>
        <item>朔州市</item>
        <item>晋中市</item>
        <item>运城市</item>
    </string-array>
    <string-array name="fj">
        <item>福州市</item>
        <item>厦门市</item>
        <item>泉州市</item>
        <item>漳州市</item>
        <item>福清市</item>
        <item>莆田市</item>
        <item>宁德市</item>
    </string-array>
    <string-array name="guangdong">
        <item>广州市</item>
        <item>深圳市</item>
        <item>中山市</item>
        <item>佛山市</item>
        <item>惠州市</item>
        <item>珠海市</item>
        <item>汕头市</item>
    </string-array>
    <string-array name="guizhou">
        <item>贵阳市</item>
        <item>遵义市</item>
        <item>安顺市</item>
        <item>毕节市</item>
        <item>铜仁市</item>
        <item>六盘水市</item>
    </string-array>
</resources>

大家有什么问题以及建议欢迎讨论留言。

猜你喜欢

转载自blog.csdn.net/Deng872347348/article/details/126730677