页面设计
登录页面activity_login.xml
界面截图
注册界面
activity_register.xml
界面截图
修改密码界面
activity_forget_password.xml
界面截图
这几个界面的按钮都进行的圆角的设计,可参考
button_shape.xml
这些背景颜色什么的可以根据自己需要进行设计哈,然后将其放在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文件
Words
- 云南省
- 山西省
- 福建省
- 广西壮族自治区
- 广东省
- 贵州省
- 昆明市
- 曲靖市
- 玉溪市
- 昭通市
- 保山市
- 丽江市
- 南宁市
- 桂林市
- 柳州市
- 北海市
- 钦州市
- 玉林市
- 百色市
- 河池市
- 防城港市
- 大同市
- 临汾市
- 太原市
- 阳泉市
- 长治市
- 晋城市
- 朔州市
- 晋中市
- 运城市
- 福州市
- 厦门市
- 泉州市
- 漳州市
- 福清市
- 莆田市
- 宁德市
- 广州市
- 深圳市
- 中山市
- 佛山市
- 惠州市
- 珠海市
- 汕头市
- 贵阳市
- 遵义市
- 安顺市
- 毕节市
- 铜仁市
- 六盘水市
大家有什么问题以及建议欢迎讨论留言。