新建一个空的项目
到这里我们的项目就搭建完成了,接下来就来编写app的页面
提前准备
1.新建一个 登录 的java和xml文件
2.同样的步骤新建一个忘记登录的页面
3.创建一个工具类,这个工具来主要是用来隐藏软键盘的
import android.app.Activity; import android.content.Context; import android.view.View; import android.view.inputmethod.InputMethodManager; public class ViewTuil { public static void hidekey(Activity a, View v){ InputMethodManager im =(InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(v.getWindowToken(),0); } }
4.在drawable文件下导入背景图
5.在values目录下定义我们需要的文字和颜色
colors
<color name="grey">#cccccc</color> <color name="red">#BF1206</color>
strings
<string name="login_by_password">密码登录</string> <string name="login_by_verifycode">验证码登录</string> <string name="phone_number">手机号码:</string> <string name="input_phone_number">请输入手机号码</string> <string name="login_password">登录密码:</string> <string name="input_password">请输入密码:</string> <string name="forget_password">忘记密码</string> <string name="remember_password">记住密码</string> <string name="login">登      录</string> <string name="input_new_password">输入新密码:</string> <string name="input_new_password_hint">请输入新密码</string> <string name="confirm_new_password">确认新密码:</string> <string name="input_new_password_again">请再次输入新密码</string> <string name="verifycode">    验证码:</string> <string name="verifycode2">        验证码:</string> <string name="input_verifycode">请输入验证码</string> <string name="get_verifycode">获取验证码</string> <string name="done">确     定</string> <string name="main">欢迎来到主页面</string> <string name="ret">返回</string> <string name="get">获取验证码</string>
新建一个负责文字大小的xml
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="csize">17sp</dimen> <dimen name="bsize">20sp</dimen> <dimen name="lsize">50sp</dimen> </resources>
6.修改activity_main的页面
<?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:padding="10dp" android:orientation="vertical" android:background="@drawable/bg" tools:context=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="@dimen/lsize" android:layout_marginTop="360dp" android:gravity="center" android:text="@string/main" android:textColor="@color/red" android:textSize="@dimen/bsize" /> <Button android:id="@+id/ret" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/ret" android:layout_marginTop="60dp"></Button> </LinearLayout>
修改后的样式如下图所示
到这里我们的准备工作就算完成了
1.编写activity_login的页面(如果没有需求请不要修改id的名称)
<?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=".LoginActivity"> <RadioGroup android:id="@+id/login" android:layout_width="match_parent" android:layout_height="@dimen/lsize" android:orientation="horizontal"> <RadioButton android:id="@+id/word" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="@string/login_by_password" android:textSize="@dimen/csize" android:checked="true"/> <RadioButton android:id="@+id/veryword" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:text="@string/login_by_verifycode" android:textSize="@dimen/csize"/> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/lsize" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="@string/phone_number" android:textColor="@color/black" android:textSize="@dimen/csize" /> <EditText android:id="@+id/etphone" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:hint="@string/input_phone_number" android:inputType="number" android:maxLength="11" android:textColor="@color/black" android:textColorHint="@color/grey" android:textSize="@dimen/csize"> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/lsize" android:orientation="horizontal"> <TextView android:id="@+id/tvword" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="@string/login_password" android:textColor="@color/black" android:textSize="@dimen/csize" /> <RelativeLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> <EditText android:id="@+id/etword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:hint="@string/input_password" android:inputType="numberPassword" android:maxLength="11" android:textColor="@color/black" android:textColorHint="@color/grey" android:textSize="@dimen/csize"> </EditText> <Button android:id="@+id/btnforget" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="@string/forget_password" android:layout_alignParentRight="true" android:textColor="@color/black" android:textSize="@dimen/csize"> </Button> </RelativeLayout> </LinearLayout> <CheckBox android:id="@+id/ckremember" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/remember_password" android:textColor="@color/black" android:textSize="@dimen/csize" /> <Button android:id="@+id/btnremember" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/login" android:textColor="@color/black" android:textSize="@dimen/bsize" /> </LinearLayout>
2.编写登录页面的逻辑
public class LoginActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { private TextView tvword; private EditText etword ; private Button btnforget; private CheckBox ckremember; private EditText etphone; private RadioButton word; private RadioButton veryword; private Button btnremember; private String code; private String www = "123456"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); word = findViewById(R.id.word);//密码登录 veryword = findViewById(R.id.veryword);//验证码登录 RadioGroup login = findViewById(R.id.login);//按钮控件 etphone = findViewById(R.id.etphone);//手机号 etword = findViewById(R.id.etword);//输入的密码 tvword = findViewById(R.id.tvword);//密码登录 btnforget = findViewById(R.id.btnforget);//忘记密码 ckremember = findViewById(R.id.ckremember);//记住密码 btnremember = findViewById(R.id.btnremember);//登录 etphone.addTextChangedListener(new HideTextWatcher(etphone,11)); etword.addTextChangedListener(new HideTextWatcher(etword,6)); btnforget.setOnClickListener(this);//忘记密码 btnremember.setOnClickListener(this);//登录 //给login设置单选监听器 login.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId){ case R.id.word: tvword.setText(getString(R.string.login_password)); etword.setHint(getString(R.string.input_new_password)); btnforget.setText(getString(R.string.forget_password)); ckremember.setVisibility(View.VISIBLE); break; case R.id.veryword: tvword.setText(getString(R.string.verifycode)); etword.setHint(getString(R.string.input_verifycode)); btnforget.setText(getString(R.string.get_verifycode)); ckremember.setVisibility(View.GONE); break; } } @Override public void onClick(View v) { String phone = etphone.getText().toString(); switch (v.getId()){ case R.id.btnforget: if(phone.length()<11){ Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show(); return; } if(word.isChecked()){ Intent intent = new Intent(this,ForgetWordActivity.class); intent.putExtra("phone",phone); startActivityForResult(intent,100); }else if(veryword.isChecked()){ code = String.format("%06d",new Random().nextInt(999999)); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请记住验证码"); builder.setMessage("手机号:"+phone+"\t,本次的验证码"+code+"\t,请输入验证码"); builder.setPositiveButton("好的",null); AlertDialog dialog = builder.create(); dialog.show(); } break; case R.id.btnremember: if(word.isChecked()){ if(www.equals(etword.getText().toString())){ loginSuccess(); }else { Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show(); return; } }else if(veryword.isChecked()) { if(code.equals(etword.getText().toString())){ loginSuccess(); }else { Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show(); return; } } break; } } private void loginSuccess() { String desc =String.format("您的手机号码是%s ,恭喜您通过登录验证,点击'确定'按钮返回上个页面",etphone.getText().toString()); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("登录成功"); builder.setMessage(desc); builder.setPositiveButton("确定返回", ( dialog, which)-> { finish(); }); AlertDialog.Builder builder1 = builder.setNegativeButton("继续看看", ( dialog, which)-> { startActivity(new Intent(this,MainActivity.class)); }); AlertDialog dialog = builder.create(); dialog.show(); } //会自动接收目标页面回传的值 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 100 && resultCode == 200 ){ String revalue = data.getStringExtra("msg"); www=revalue; } } //隐藏输入法 private class HideTextWatcher implements TextWatcher { private int maxLength; private EditText et; public HideTextWatcher(EditText etphone, int i) { this.et=etphone; this.maxLength=i; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable s) { //隐藏输入法 if(s.toString().length() ==maxLength){ ViewTuil.hidekey(LoginActivity.this,et); } } } }
3.返回登录页面(放在MainActivity的onCreate方法中)
findViewById(R.id.ret).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } });
4.编写忘记密码的页面
<?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=".ForgetWordActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="@dimen/lsize" android:text="@string/input_password" android:textSize="@dimen/csize" android:textColor="@color/black" android:padding="15dp" /> <EditText android:id="@+id/one" android:layout_width="0dp" android:layout_height="@dimen/lsize" android:layout_weight="1" android:inputType="numberPassword" android:hint="@string/input_new_password_hint"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="@dimen/lsize" android:text="@string/input_new_password" android:textSize="@dimen/csize" android:textColor="@color/black" android:padding="15dp" /> <EditText android:id="@+id/two" android:layout_width="0dp" android:layout_height="@dimen/lsize" android:layout_weight="1" android:inputType="numberPassword" android:hint="@string/input_new_password_again"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="@dimen/lsize" android:text="@string/verifycode2" android:textSize="@dimen/csize" android:textColor="@color/black" android:padding="15dp" /> <EditText android:id="@+id/code" android:layout_width="0dp" android:layout_height="@dimen/lsize" android:layout_weight="1" android:hint="@string/input_new_password_again"/> <Button android:id="@+id/btncode" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="@string/get"/> </LinearLayout> <Button android:id="@+id/modify" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/done" android:textColor="@color/black" android:textSize="@dimen/csize"></Button> </LinearLayout>
5..编写忘记密码的逻辑(我的参数跳转使用的是老方法,老方法没有过期)
public class ForgetWordActivity extends AppCompatActivity implements View.OnClickListener { private EditText one; private EditText two; private EditText code; private String phone; private String verycode=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_forget_word); one = findViewById(R.id.one);//密码 two = findViewById(R.id.two);//确认密码 code = findViewById(R.id.code);//验证码 Button btncode = findViewById(R.id.btncode);//获取验证码 Button modify = findViewById(R.id.modify);//修改 btncode.setOnClickListener(this); modify.setOnClickListener(this); } @Override public void onClick(View v) { phone = getIntent().getStringExtra("phone"); switch (v.getId()){ case R.id.btncode: verycode = String.format("%06d",new Random().nextInt(999999));//生成6位数的随机数 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请记住验证码");//设置弹出的标题 builder.setMessage("手机号:"+phone+"\n本次的验证码"+verycode+"\n请输入验证码");//设置弹出的内容 builder.setPositiveButton("好的",null);//设置弹出的按钮 AlertDialog dialog = builder.create(); dialog.show();//弹出 break; case R.id.modify: String one1 = one.getText().toString(); String two1 = two.getText().toString(); if(one1.length()<6){ Toast.makeText(this, "您的密码长度必须大于等于6位", Toast.LENGTH_SHORT).show(); return; } if(!one1.equals(two1)){ System.out.println("不一致"); Toast.makeText(this, "您两次输入的密码不一致", Toast.LENGTH_SHORT).show(); return; } if("".equals(verycode)){ Toast.makeText(this, "请您先获取验证码", Toast.LENGTH_SHORT).show(); return; } if(!verycode.equals(code.getText().toString())){ System.out.println("验证码错误"); Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show(); return; } Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(); intent.putExtra("msg",one1); //携带数据回到源页面 setResult(200,intent); //已经回去了 finish(); break; } } }
将主页面改为LoginActivity
如果主页面不适配的话可以添加以下代码,可以自动适配屏幕大小
getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_FULLSCREEN| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
效果展示图
1.密码登录(初始密码)
2.验证码的登录
3.忘记密码