需求:增加注册按钮,能够在点击后弹出注册界面并完成简单的注册功能,账号密码输入成功后可以跳转页面。
一、实现注册的FragmentDialog界面 1.新建一个界面,命名为create_dialog,并在其中写入所需要的与注册相关的控件 1.1.添加背景这里就不放入图片了,直接添加一个空白背景
1.2.添加用于注册的控件、确认注册和取消注册的按钮
怎么来写这些东西在前两篇笔记中写了,这里就不再细说,直接上代码了
2.写好界面对应的java代码 2.1.了解SharedPreferences储存信息的方法
SharedPreferences存储、查看_u013441613的博客-CSDN博客_sharedpreferences存储位置
SharedPreferences只能储存少量信息,不建议将大量数据塞进SharedPreferences内储存。
这里我们要用到的主要有.remove``.putString``.apply方法。
2.2.创建CreateDialog并继承DialogFragment类、实现View.OnClickListener接口public class CreateDialog extends DialogFragment implements View.OnClickListener {
View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static CreateDialog newInstance() {
Bundle args = new Bundle();
CreateDialog fragment = new CreateDialog();
fragment.setArguments(args);
return fragment;
}
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// 加载 xml 布局
view = inflater.inflate(R.layout.create_dialog, null, false);
view.setElevation(30);
return view;
}
@Override
public void onClick(View v) {
}
}
2.3.绑定控件并设置监听
2.3.1.绑定所需的控件到相应对象
private EditText mEtUsername;
private EditText mEtPassword1;
private EditText mEtPassword2;
private Button mBtnCreate;
private Button mBtnCancel;
//绑定控件
public void initView(){
mEtUsername = view.findViewById(R.id.et_dialog_username);
mEtPassword1 = view.findViewById(R.id.et_dialog_password1);
mEtPassword2 = view.findViewById(R.id.et_dialog_password2);
mBtnCreate = view.findViewById(R.id.btn_dialog_yes);
mBtnCancel = view.findViewById(R.id.btn_dialog_cancel);
}
2.3.2.设置监听
//设置
public void setView(){
mBtnCreate.setOnClickListener(this);
mBtnCancel.setOnClickListener(this);
}
2.4.SharedPreferences、注册判断与数据写入
2.4.1.创建并绑定SharedPreferences
SharedPreferences sharedPreferences; SharedPreferences.Editor editor;
sharedPreferences = requireContext().getSharedPreferences("Data", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
2.4.2.注册数据判断与数据写入
首先判断用户名是否为空,如果为空则弹出“吐司”提示用户民不能为空
其次,判断两次输入的密码是否匹配,匹配则写入新数据并关闭此Dialog,失败则提示两次密码不匹配
实现代码:
public void create(){
String username = mEtUsername.getText().toString();
String password1 = mEtPassword1.getText().toString();
String password2 = mEtPassword2.getText().toString();
if(username.equals("")){
Toast.makeText(getActivity(), "用户名不能为空!", Toast.LENGTH_SHORT).show();
}else if(!password1.equals(password2)){
Toast.makeText(getActivity(),"两次密码不匹配,请重新检查!",Toast.LENGTH_SHORT).show();
}else{
//清除原数据并写入新数据
editor.remove("username");
editor.remove("password");
editor.putString("username",username);
editor.putString("password",password1);
editor.apply();
Toast.makeText(getActivity(), "注册成功", Toast.LENGTH_SHORT).show();
//关闭注册窗口
dismiss();
}
}
2.5.设置点击事件
设置点击事件,选择注册则进入注册方法进行判断,否则关闭Dialog窗口
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_dialog_yes:
create();
break;
case R.id.btn_dialog_cancel:
dismiss();
break;
}
}
3.在主界面添加注册按键并在MainActivity设置点击弹出注册窗口
弹出窗口代码:CreateDialog.newInstance().show(getSupportFragmentManager(), "dialog");
4.效果注册点击与取消注册点击:
账号判断:
用户名为空
密码不匹配
注册成功
二、实现页面跳转首先,写好一个界面用于跳转后显示(这里就简单写一个意思一下)
在demo的java代码里面写上一个静态startActivity方法用来给主界面连接跳转
public static void startActivity(Context context){
Intent intent = new Intent(context,DemoActivity.class);
context.startActivity(intent);
}
在主界面java代码中写上点击登录成功则跳转界面的代码
public void login(){
String username = mEtUsername.getText().toString();
String password = mEtPassword.getText().toString();
if(!username.equals(sharedPreferences.getString("username",""))){
Toast.makeText(this,"用户名错误",Toast.LENGTH_SHORT).show();
}else if(!password.equals(sharedPreferences.getString("password",""))){
Toast.makeText(this,"您的密码错误",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
DemoActivity.startActivity(this);
}
}
效果图
三、完整代码 1.activity_main.xml2.MainActivity.java
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText mEtUsername;
private EditText mEtPassword;
private Button mBtnLogin;
private Button mBtnCreate;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setView();
}
public void initView() {
mEtUsername = findViewById(R.id.et_main_username);
mEtPassword = findViewById(R.id.et_main_password);
mBtnLogin = findViewById(R.id.btn_main_login);
mBtnCreate = findViewById(R.id.btn_main_create);
sharedPreferences = getSharedPreferences("Data", Context.MODE_PRIVATE);
}
public void setView(){
mBtnLogin.setOnClickListener(this);
mBtnCreate.setOnClickListener(this);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_main_login:
login();
break;
case R.id.btn_main_create:
CreateDialog.newInstance().show(getSupportFragmentManager(), "dialog");
break;
}
}
public void login(){
String username = mEtUsername.getText().toString();
String password = mEtPassword.getText().toString();
if(!username.equals(sharedPreferences.getString("username",""))){
Toast.makeText(this,"用户名错误",Toast.LENGTH_SHORT).show();
}else if(!password.equals(sharedPreferences.getString("password",""))){
Toast.makeText(this,"您的密码错误",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
DemoActivity.startActivity(this);
}
}
}
3.create_dialog.xml
4.CreateDialog.java
package com.example.demo;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class CreateDialog extends DialogFragment implements View.OnClickListener {
View view;
private EditText mEtUsername;
private EditText mEtPassword1;
private EditText mEtPassword2;
private Button mBtnCreate;
private Button mBtnCancel;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static CreateDialog newInstance() {
Bundle args = new Bundle();
CreateDialog fragment = new CreateDialog();
fragment.setArguments(args);
return fragment;
}
@SuppressLint("InflateParams")
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// 加载 xml 布局
view = inflater.inflate(R.layout.create_dialog, null, false);
view.setElevation(30);
initView();
setView();
return view;
}
//绑定控件
public void initView(){
mEtUsername = view.findViewById(R.id.et_dialog_username);
mEtPassword1 = view.findViewById(R.id.et_dialog_password1);
mEtPassword2 = view.findViewById(R.id.et_dialog_password2);
mBtnCreate = view.findViewById(R.id.btn_dialog_yes);
mBtnCancel = view.findViewById(R.id.btn_dialog_cancel);
sharedPreferences = requireContext().getSharedPreferences("Data", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
}
//设置
public void setView(){
mBtnCreate.setOnClickListener(this);
mBtnCancel.setOnClickListener(this);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_dialog_yes:
create();
break;
case R.id.btn_dialog_cancel:
dismiss();
break;
}
}
public void create(){
String username = mEtUsername.getText().toString();
String password1 = mEtPassword1.getText().toString();
String password2 = mEtPassword2.getText().toString();
if(username.equals("")){
Toast.makeText(getActivity(), "用户名不能为空!", Toast.LENGTH_SHORT).show();
}else if(!password1.equals(password2)){
Toast.makeText(getActivity(),"两次密码不匹配,请重新检查!",Toast.LENGTH_SHORT).show();
}else{
//清除原数据并写入新数据
editor.remove("username");
editor.remove("password");
editor.putString("username",username);
editor.putString("password",password1);
editor.apply();
Toast.makeText(getActivity(), "注册成功", Toast.LENGTH_SHORT).show();
//关闭注册窗口
dismiss();
}
}
}
5.activity_demo.xml
6.DemoActivity.java
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class DemoActivity extends AppCompatActivity {
public static void startActivity(Context context){
Intent intent = new Intent(context,DemoActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
}
}



