login登录注册页面-页面布局系列

  • 完整代码ZIP:下载
  • Android技术生活-QQ交流群:723592501
  • 更多其他页面-自定义View-实用功能合集:点击查看


功能简介

简单的实现登录注册页面,提供思路


实现步骤

1.创建登录页面,注册页面,登陆成功后的页面。
2.没有账号进行注册,将注册的账号储存起来key-账号,value-密码。
3.登陆时,判断输入的账号密码是否与储存的一致


GIF演示

在这里插入图片描述


java代码 LoginActivity

/**
 * 作者:YFZ
 * 简介:简单的登陆注册页面,代码清晰简介便于理解。
 * Android技术生活-QQ交流群:723592501
 */
public class LoginActivity extends AppCompatActivity {
    private EditText mEdtAccount,mEdtPassword;

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

    private void initialView(){
        mEdtAccount=findViewById(R.id.edtAccount);
        mEdtPassword=findViewById(R.id.edtPassword);
    }

    /**
     * 点击注册按钮-前往注册页面
     * @param view
     */
    public void goToRegisterActivity(View view) {
        Intent intent=new Intent(this,RegisterActivity.class);
        startActivity(intent); //跳转到注册页面
    }

    /**
     * 点击登陆按钮-比对账号密码是否一致
     * @param view
     */
    public void doLogin(View view) {
        String userAccount=mEdtAccount.getText().toString(); //输入的账号
        String userInputPassword= mEdtPassword.getText().toString(); //输入的密码
        String userPassword = SPUtil.getUserPassword(this,mEdtAccount.getText().toString());//从sp储存中以账号作为key,取出对应的value(密码)
        if(userAccount.length()==0){ //没有输入账号
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if(userInputPassword.length()==0){ //没有输入密码
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if(userPassword==null){  //如果没有对应的账号
            Toast.makeText(this, "账号不存在", Toast.LENGTH_SHORT).show();
            return;
        }
      if(userPassword.equals(userInputPassword)){ //比对输入的密码,和储存的密码是否一致
          Toast.makeText(this, "密码一致,登陆成功", Toast.LENGTH_SHORT).show();
          Intent intent=new Intent(this,UserActivity.class);
          startActivity(intent);
      }else {
          Toast.makeText(this, "密码不一致,登陆失败", Toast.LENGTH_SHORT).show();
      }
    }
}

java代码 RegisterActivity

/**
 * 作者:游丰泽
 * 简介:简单的登陆注册页面,代码清晰简介便于理解。
 * Android需求与交流请加wx:yfz_oom
 */
public class RegisterActivity extends AppCompatActivity {
    private EditText mEdtAccount,mEdtPassword,mEdtPasswordConfirm;

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

    private void initialView(){
        mEdtAccount=findViewById(R.id.edtAccount);
        mEdtPassword=findViewById(R.id.edtPassword);
        mEdtPasswordConfirm=findViewById(R.id.edtPasswordConfirm);
    }


    /**
     * 点击注册按钮-进行注册-将账号和密码储存到sp中
     * @param view
     */
    public void doRegister(View view) {
        String userAccount=mEdtAccount.getText().toString(); //输入的账号
        String userPassword=mEdtPassword.getText().toString(); //输入的密码
        String userPasswordConfirm=mEdtPasswordConfirm.getText().toString(); //再次输入的确认密码
        if(userAccount.length()==0){
            Toast.makeText(this,"账号不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(userPassword.length()==0){
            Toast.makeText(this,"密码不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(userPasswordConfirm.length()==0){
            Toast.makeText(this,"再次输入确认密码不可为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if( ! userPassword.equals(userPasswordConfirm)){
            Toast.makeText(this,"两次输入密码不一致",Toast.LENGTH_SHORT).show();
            return;
        }

        SPUtil.saveUserDataInSharePreference(this,userAccount,userPassword);

        Toast.makeText(this,"注册成功,返回",Toast.LENGTH_SHORT).show();

        Intent intent=new Intent(this,LoginActivity.class);
        startActivity(intent);

    }

}

java代码 SPUtil


public class SPUtil {
    public static void saveUserDataInSharePreference(Activity activity,String account,String password){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("userData", activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(account, password); //key-账号 对应 value-密码
        editor.apply();
    }
    public static String getUserPassword(Activity activity,String account){
        SharedPreferences sharedPreferences = activity.getSharedPreferences("userData", activity.MODE_PRIVATE);
        return sharedPreferences.getString(account,null); //通过key获取对应的密码
    }
}


java代码 UserActivity

public class UserActivity extends AppCompatActivity {

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

java代码 activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#f6f5ec"
    tools:context=".LoginActivity">
    <TextView
        android:id="@+id/txTittle"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="简单的登录注册页面,代码清晰便于理解。\nAndroid需求和交流请加wx:  yfz_oom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
    </TextView>
    <androidx.constraintlayout.widget.ConstraintLayout
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="24dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tvLoginPage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:textSize="20dp"
            android:textStyle="bold"
            android:text="登录页面" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clAccount"
            app:layout_constraintTop_toBottomOf="@+id/tvLoginPage"
            android:layout_marginTop="24dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvAccount"
                android:text="账号"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <EditText
                android:id="@+id/edtAccount"
                app:layout_constraintTop_toBottomOf="@+id/tvAccount"
                android:layout_width="match_parent"
                android:layout_height="@dimen/ed_height">
            </EditText>
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clPassword"
            app:layout_constraintTop_toBottomOf="@+id/clAccount"
            android:layout_marginTop="24dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvPassword"
                android:text="密码"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <EditText
                android:id="@+id/edtPassword"
                app:layout_constraintTop_toBottomOf="@+id/tvPassword"
                android:layout_width="match_parent"
                android:layout_height="@dimen/ed_height">
            </EditText>
        </androidx.constraintlayout.widget.ConstraintLayout>

        <Button
            android:id="@+id/btnLogin"
            android:text="点击登录"
            android:onClick="doLogin"
            app:layout_constraintTop_toBottomOf="@+id/clPassword"
            android:layout_width="match_parent"
            android:layout_height="@dimen/ed_height">
        </Button>
        <Button
            android:id="@+id/btnRegister"
            android:text="没有账号?前往注册"
            android:onClick="goToRegisterActivity"
            app:layout_constraintTop_toBottomOf="@+id/btnLogin"
            android:layout_width="match_parent"
            android:layout_height="@dimen/ed_height">
        </Button>
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>



java代码 activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#f6f5ec"
    tools:context=".RegisterActivity">
    <TextView
        android:id="@+id/txTittle"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:text="简单的登录注册页面,代码清晰便于理解。\nAndroid需求和交流请加wx:  yfz_oom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
    </TextView>
    <androidx.constraintlayout.widget.ConstraintLayout
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="24dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tvLoginPage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:textSize="20dp"
            android:textStyle="bold"
            android:text="注册页面" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clAccount"
            app:layout_constraintTop_toBottomOf="@+id/tvLoginPage"
            android:layout_marginTop="24dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvAccount"
                android:text="账号"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <EditText
                android:id="@+id/edtAccount"
                app:layout_constraintTop_toBottomOf="@+id/tvAccount"
                android:layout_width="match_parent"
                android:layout_height="@dimen/ed_height">
            </EditText>
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clPassword"
            app:layout_constraintTop_toBottomOf="@+id/clAccount"
            android:layout_marginTop="24dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvPassword"
                android:text="密码"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <EditText
                android:id="@+id/edtPassword"
                app:layout_constraintTop_toBottomOf="@+id/tvPassword"
                android:layout_width="match_parent"
                android:layout_height="@dimen/ed_height">
            </EditText>
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clPasswordConfirm"
            app:layout_constraintTop_toBottomOf="@+id/clPassword"
            android:layout_marginTop="24dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tvPasswordConfirm"
                android:text="再次输入密码"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <EditText
                android:id="@+id/edtPasswordConfirm"
                app:layout_constraintTop_toBottomOf="@+id/tvPasswordConfirm"
                android:layout_width="match_parent"
                android:layout_height="@dimen/ed_height">
            </EditText>
        </androidx.constraintlayout.widget.ConstraintLayout>


        <Button
            android:id="@+id/btnRegister"
            android:text="点击注册"
            android:onClick="doRegister"
            app:layout_constraintTop_toBottomOf="@+id/clPasswordConfirm"
            android:layout_width="match_parent"
            android:layout_height="@dimen/ed_height">
        </Button>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>



java代码 activity_user.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#f6f5ec"
    tools:context=".UserActivity">
    <TextView
        android:text="登陆成功"
        android:textSize="24dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </TextView>

</androidx.constraintlayout.widget.ConstraintLayout>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值