主页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0">
</FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textColor="#8E8433"
android:layout_marginLeft="20dp"
android:text="密码登录"
/>
<RadioButton
android:id="@+id/rb_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#8E8433"
android:textSize="15dp"
android:layout_marginRight="20dp"
android:text="验证码登录"
android:onClick="jumpToMainActivity2"
/>
</RadioGroup>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="账号:"
android:textColor="#D30F64"
android:textSize="25sp"
/>
<EditText
android:id="@+id/et_account"
android:layout_width="269dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="@drawable/edit_text_bg"
android:hint="请输入手机号或用户名"
android:inputType="text"
android:paddingLeft="5dp"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="90dp"
android:layout_height="wrap_content"
android:textColor="#D30F64"
android:text="密码:"
android:textSize="25sp"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="180dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="@drawable/edit_text_bg"
android:hint="请输入密码"
android:inputType="numberPassword"
android:paddingLeft="5dp"
android:textSize="18sp" />
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="忘记密码"
android:textSize="13dp"
android:onClick="jumpToMainActivity3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:layout_marginLeft="40dp"
/>
</LinearLayout>
<Button
android:id="@+id/btn_Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="25sp"
/>
</LinearLayout>
手机验证码页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity2">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textColor="#8E8433"
android:layout_marginLeft="20dp"
android:text="密码登录"
android:onClick="jumpToMainActivity"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="15dp"
android:textColor="#8E8433"
android:layout_marginRight="20dp"
android:text="验证码登录"/>
</RadioGroup>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="手机号码:"
android:textColor="#D30F64"
android:textSize="20sp"
/>
<EditText
android:id="@+id/et_phone"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="@drawable/edit_text_bg"
android:hint="请输入11位数手机号"
android:inputType="number"
android:paddingLeft="5dp"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp"
android:gravity="center_vertical"
>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="验证码:"
android:textColor="#D30F64"
android:textSize="20dp"
/>
<EditText
android:id="@+id/et_confirm"
android:layout_width="180dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:background="@drawable/edit_text_bg"
android:hint="请输入验证码"
android:inputType="number"
android:paddingLeft="5dp"
android:textSize="18sp" />
<Button
android:id="@+id/btn_getNum"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="获取验证码"
android:textSize="11dp"
android:onClick="jumpToAlertDialogActivity"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录"
android:layout_marginLeft="40dp"
/>
</LinearLayout>
<Button
android:id="@+id/btn_Login1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textSize="25sp"
/>
</LinearLayout>
找回密码页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tv_password_first"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="输入新密码:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_password_first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/tv_password_first"
android:gravity="left|center"
android:hint="请输入新密码"
android:inputType="numberPassword"
android:maxLength="11"
android:textColor="@color/black"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tv_password_second"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="确认新密码:"
android:textColor="@color/black"
android:textSize="17sp" />
<EditText
android:id="@+id/et_password_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/tv_password_second"
android:gravity="left|center"
android:hint="请再次输入新密码"
android:inputType="numberPassword"
android:maxLength="11"
android:textColor="@color/black"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tv_verifycode"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text=" 验证码:"
android:textColor="@color/black"
android:textSize="17sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_verifycode" >
<EditText
android:id="@+id/et_verifycode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="left|center"
android:hint="请输入验证码"
android:inputType="number"
android:maxLength="6"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:id="@+id/btn_verifycode"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="-3dp"
android:gravity="center"
android:text="获取验证码"
android:textColor="@color/black"
android:textSize="17sp" />
</RelativeLayout>
</RelativeLayout>
<Button
android:id="@+id/btn_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确 定"
android:textColor="@color/black"
android:textSize="20sp" />
</LinearLayout>
MainActivity
package com.example.retrievepassword;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "tag";
private Button btnLogin;
private EditText etAccount, etPassword;
String newPass,MPhone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("用户名登录");
btnLogin = findViewById(R.id.btn_Login);
etAccount = findViewById(R.id.et_account);
etPassword = findViewById(R.id.et_password);
etAccount = findViewById(R.id.et_account);
btnLogin.setOnClickListener(this);
newPass = getIntent().getStringExtra("new_password");
MPhone = getIntent().getStringExtra("phone");
Log.d(TAG, "onCreate: --------------"+newPass);
Log.d(TAG, "onCreate: --------------"+MPhone);
}
@Override
public void onClick(View v) {
String Account = etAccount.getText().toString();
String Pass = etPassword.getText().toString();
if(!Account.equals(MPhone)){
Toast.makeText(this, "用户名错误" , Toast.LENGTH_SHORT).show();
}else if(!Pass.equals(newPass)){
Toast.makeText(this, "新密码错误" , Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(this, "恭喜您!登陆成功" , Toast.LENGTH_SHORT).show();
}
}
public void jumpToMainActivity2 (View view){
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
public void jumpToMainActivity3 (View view){
String s = etAccount.getText().toString().trim();
Intent intent = new Intent(this, LoginForgetActivity.class);
intent.putExtra("phone", s);
startActivity(intent);
}
}
验证码的Activity
package com.example.retrievepassword;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.retrievepassword.util.ViewUtil;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "tag";
private String confirm;
private EditText et_verConfirm,etPhone;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
getSupportActionBar().setTitle("验证码登录");
Intent intent = getIntent();
btnLogin = findViewById(R.id.btn_Login1);
etPhone = findViewById(R.id.et_phone);
et_verConfirm = findViewById(R.id.et_confirm);
if(intent != null){
confirm = intent.getStringExtra("data");
Log.d(TAG, "onCreate :----------------"+confirm);
}
// 给手机号码编辑框添加文本变化监听器
etPhone.addTextChangedListener(new MainActivity2.HideTextWatcher(etPhone, 11));
btnLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String con = et_verConfirm.getText().toString();
String mPhone = etPhone.getText().toString();
if(mPhone != null && TextUtils.equals(con,confirm)){
Toast.makeText(MainActivity2.this,"恭喜您!登陆成功",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity2.this,"验证码错误!",Toast.LENGTH_LONG).show();
}
}
public void jumpToAlertDialogActivity(View view) {
Intent intent = new Intent(this,AlertDialogActivity.class);
startActivity(intent);
}
private class HideTextWatcher implements TextWatcher {
private EditText mView; // 声明一个编辑框对象
private int mMaxLength; // 声明一个最大长度变量
public HideTextWatcher(EditText v, int maxLength) {
super();
mView = v;
mMaxLength = maxLength;
}
// 在编辑框的输入文本变化前触发
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
// 在编辑框的输入文本变化时触发
public void onTextChanged(CharSequence s, int start, int before, int count) {}
// 在编辑框的输入文本变化后触发
public void afterTextChanged(Editable s) {
String str = s.toString(); // 获得已输入的文本字符串
// 输入文本达到11位(如手机号码),或者达到6位(如登录密码)时关闭输入法
if ((str.length() == 11 && mMaxLength == 11)) {
ViewUtil.hideOneInputMethod(MainActivity2.this, mView); // 隐藏输入法软键盘
}
}
}
public void jumpToMainActivity(View view) {
Intent intent = new Intent(this,MainActivity.class);
startActivity(intent);
}
}
忘记密码的Activity
package com.example.retrievepassword;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;
public class LoginForgetActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_password_first; // 声明一个编辑框对象
private EditText et_password_second; // 声明一个编辑框对象
private EditText et_verifyCode; // 声明一个编辑框对象
private String mVerifyCode; // 验证码
private String mPhone; // 手机号码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_forget);
// 从布局文件中获取名叫et_password_first的编辑框
getSupportActionBar().setTitle("找回密码");
et_password_first = findViewById(R.id.et_password_first);
// 从布局文件中获取名叫et_password_second的编辑框
et_password_second = findViewById(R.id.et_password_second);
// 从布局文件中获取名叫et_verifycode的编辑框
et_verifyCode = findViewById(R.id.et_verifycode);
findViewById(R.id.btn_verifycode).setOnClickListener(this);
findViewById(R.id.btn_confirm).setOnClickListener(this);
// 从上一个页面获取要修改密码的手机号码
mPhone = getIntent().getStringExtra("phone");
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_verifycode) { // 点击了“获取验证码”按钮
if (mPhone == null || mPhone.length() < 11) {
Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
return;
}
// 生成六位随机数字的验证码
mVerifyCode = String.format("%06d", new Random().nextInt(999999));
// 以下弹出提醒对话框,提示用户记住六位验证码数字
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请记住验证码");
builder.setMessage("手机号" + mPhone + ",本次验证码是" + mVerifyCode + ",请输入验证码");
builder.setPositiveButton("好的", null);
AlertDialog alert = builder.create();
alert.show(); // 显示提醒对话框
} else if (v.getId() == R.id.btn_confirm) { // 点击了“确定”按钮
String password_first = et_password_first.getText().toString();
String password_second = et_password_second.getText().toString();
if (password_first.length() < 6 || password_second.length() < 6) {
Toast.makeText(this, "请输入正确的新密码", Toast.LENGTH_SHORT).show();
return;
}
if (!password_first.equals(password_second)) {
Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
return;
}
if (!et_verifyCode.getText().toString().equals(mVerifyCode)) {
Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
// 以下把修改好的新密码返回给上一个页面
Intent intent = new Intent(this,MainActivity.class); // 创建一个新意图
intent.putExtra("new_password", password_first); // 存入新密码
intent.putExtra("phone",mPhone);
startActivity(intent);
}
}
}
}