欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现简易登陆注册逻辑的实例代码

程序员文章站 2022-03-19 09:33:02
大家好,今天给大家带来android制作登录和注册功能的实现,当我们面临制作登录和注册功能的实现时,我们需要先设计登录界面的布局和注册界面的布局,做到有完整的思路时才开始实现其功能效果会更好。acti...

大家好,今天给大家带来android制作登录和注册功能的实现,当我们面临制作登录和注册功能的实现时,我们需要先设计登录界面的布局和注册界面的布局,做到有完整的思路时才开始实现其功能效果会更好。

Android实现简易登陆注册逻辑的实例代码

Android实现简易登陆注册逻辑的实例代码

Android实现简易登陆注册逻辑的实例代码

activity_login

<?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"
    tools:context=".mainactivity">

    <textview
        android:id="@+id/tv_username"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginstart="40dp"
        android:layout_margintop="100dp"
        android:gravity="center_vertical"
        android:text="账号:"
        app:layout_constraintleft_toleftof="parent"
        app:layout_constrainttop_totopof="parent" />

    <textview
        android:id="@+id/tv_password"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginstart="40dp"
        android:gravity="center_vertical"
        android:text="密码:"
        app:layout_constraintleft_toleftof="parent"
        app:layout_constrainttop_tobottomof="@id/tv_username" />

    <edittext
        android:id="@+id/ed_username"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_margintop="100dp"
        app:layout_constraintleft_torightof="@id/tv_username"
        app:layout_constrainttop_totopof="parent" />

    <edittext
        android:id="@+id/ed_password"
        android:layout_width="300dp"
        android:inputtype="textpassword"
        android:layout_height="40dp"
        app:layout_constraintleft_torightof="@id/tv_password"
        app:layout_constrainttop_tobottomof="@id/ed_username" />

    <button
        android:id="@+id/bu_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="300dp"
        android:text="登录"
        app:layout_constraintleft_toleftof="parent"
        app:layout_constraintright_toleftof="@id/bu_register"
        app:layout_constrainttop_totopof="parent" />

    <button
        android:id="@+id/bu_register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="300dp"
        android:text="注册"
        app:layout_constraintleft_torightof="@id/bu_login"
        app:layout_constraintright_torightof="parent"
        app:layout_constrainttop_totopof="parent" />

</androidx.constraintlayout.widget.constraintlayout>

loginactivity

package com.jld.exam;

import android.content.intent;
import android.os.bundle;
import android.widget.button;
import android.widget.edittext;
import android.widget.toast;

import androidx.appcompat.app.appcompatactivity;

public class loginactivity extends appcompatactivity {
    edittext ed_username;
    edittext ed_password;
    button bu_login;
    button bu_register;
    string username;
    string password;

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_login);

        ed_username = findviewbyid(r.id.ed_username);
        ed_password = findviewbyid(r.id.ed_password);
        bu_login = findviewbyid(r.id.bu_login);
        bu_register = findviewbyid(r.id.bu_register);

        //登录按钮监听
        bu_login.setonclicklistener(v -> {
            username = ed_username.gettext().tostring();
            password = ed_password.gettext().tostring();

            //登陆的简单逻辑
            if (username.equals("")) {
                toast.maketext(loginactivity.this, "请输入用户名", toast.length_short).show();
            } else if (password.equals("")) {
                toast.maketext(loginactivity.this, "请输入密码", toast.length_short).show();
            } else if (!username.equals("root") || !password.equals("123456")) {
                toast.maketext(loginactivity.this, "用户名或密码错误", toast.length_short).show();
            } else {
                intent intent = new intent(loginactivity.this, mainactivity.class);
                startactivity(intent);
            }
        });

        //注册按钮监听
        bu_register.setonclicklistener(v -> {
            intent intent = new intent(loginactivity.this, registeractivity.class);
            startactivity(intent);
        });
    }

}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <textview
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="商品列表"
        android:layout_marginbottom="30dp"
        android:gravity="center_horizontal"/>
    <androidx.recyclerview.widget.recyclerview
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</linearlayout>

mainactivity

package com.jld.exam;

import android.graphics.color;
import android.os.bundle;
import android.widget.linearlayout;

import androidx.appcompat.app.appcompatactivity;
import androidx.recyclerview.widget.gridlayoutmanager;
import androidx.recyclerview.widget.recyclerview;

public class mainactivity extends appcompatactivity {

    recyclerviewadapter recyclerviewadapter;
    private final int[] icno = {r.drawable.clock, r.drawable.signal, r.drawable.box,
            r.drawable.second, r.drawable.elephone, r.drawable.ff, r.drawable.notebook, r.drawable.mark, r.drawable.yx,
            r.drawable.shop, r.drawable.theme, r.drawable.xl,};
    private final string[] name = {"时钟", "信号", "宝箱", "秒钟", "大象", "ff", "记事本", "书签", "印象", "商店", "主题", "迅雷"};

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);//gridlayoutmanager
        gridlayoutmanager gridlayoutmanager = new gridlayoutmanager(mainactivity.this, 3);//创建recyclerview
        recyclerview recyclerview = findviewbyid(r.id.recycler);//设定布局管理器
        recyclerview.setlayoutmanager(gridlayoutmanager);//创建适配器
        recyclerviewadapter = new recyclerviewadapter(icno, name);
        recyclerview.setadapter(recyclerviewadapter);//设定适配器
    }
}

activity_register

<?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"
    tools:context=".registeractivity">

    <textview
        android:id="@+id/textview0"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="用户注册"
        android:textsize="30sp"
        app:layout_constrainttop_totopof="parent" />

    <textview
        android:id="@+id/textview1"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:drawablestart="@drawable/account"
        android:gravity="center_vertical"
        android:text="用户名:"
        app:layout_constraintleft_toleftof="parent"
        app:layout_constrainttop_tobottomof="@id/textview0" />

    <edittext
        android:id="@+id/edittext1"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_weight="10"
        android:autofillhints=""
        android:gravity="center_horizontal"
        android:inputtype="text"

        app:layout_constraintleft_torightof="@id/textview1"
        app:layout_constrainttop_tobottomof="@id/textview0" />

    <textview
        android:id="@+id/textview2"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_weight="2"
        android:drawablestart="@drawable/password"
        android:gravity="center_vertical"
        android:text="新密码:"
        app:layout_constraintleft_toleftof="parent"
        app:layout_constrainttop_tobottomof="@id/textview1" />

    <edittext
        android:id="@+id/edittext2"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_weight="10"
        android:autofillhints=""
        android:gravity="center_horizontal"
        android:inputtype="textpassword"
        app:layout_constraintleft_torightof="@id/textview2"
        app:layout_constrainttop_tobottomof="@id/edittext1" />

    <textview
        android:id="@+id/textview5"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:drawablestart="@drawable/phone"
        android:gravity="center_vertical"
        android:text="手机电话:"
        app:layout_constraintleft_toleftof="parent"
        app:layout_constrainttop_tobottomof="@id/textview2" />

    <edittext
        android:id="@+id/edittext5"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_weight="10"
        android:autofillhints=""
        android:gravity="center_horizontal"
        android:inputtype="phone"
        app:layout_constraintleft_torightof="@id/textview5"
        app:layout_constrainttop_tobottomof="@id/edittext2" />


    <textview
        android:id="@+id/textview7"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:drawablestart="@drawable/email"
        android:gravity="center_vertical"
        android:text="e_mail:"
        app:layout_constraintleft_toleftof="parent"
        app:layout_constrainttop_tobottomof="@id/edittext5" />

    <edittext
        android:id="@+id/edittext7"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_weight="10"
        android:autofillhints=""
        android:gravity="center_horizontal"
        android:inputtype="textemailaddress"
        app:layout_constraintleft_torightof="@id/textview7"
        app:layout_constrainttop_tobottomof="@id/edittext5" />

    <textview
        android:id="@+id/textview8"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:drawablestart="@drawable/gender"
        android:gravity="center_vertical"
        android:text="性别:"
        app:layout_constraintleft_toleftof="parent"
        app:layout_constrainttop_tobottomof="@id/textview7" />

    <radiogroup
        android:id="@+id/radiogroup8"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_weight="10"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        app:layout_constraintleft_torightof="@id/textview8"
        app:layout_constrainttop_tobottomof="@id/edittext7">

        <radiobutton
            android:id="@+id/radiobutton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />

        <radiobutton
            android:id="@+id/radiobutton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男" />
    </radiogroup>


    <button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="40dp"
        android:text="保存"
        app:layout_constraintend_tostartof="@id/button2"
        app:layout_constraintstart_tostartof="parent"
        app:layout_constrainttop_tobottomof="@id/radiogroup8" />

    <button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margintop="40dp"
        android:text="取消"
        app:layout_constraintend_toendof="parent"
        app:layout_constraintstart_toendof="@id/button1"
        app:layout_constrainttop_tobottomof="@id/radiogroup8" />
</androidx.constraintlayout.widget.constraintlayout>


registeractivity

package com.jld.exam;

import androidx.appcompat.app.appcompatactivity;

import android.os.bundle;

public class registeractivity extends appcompatactivity {

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_register);
    }
}

recyc_item

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <imageview
        android:id="@+id/iv_image"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal"
        android:contentdescription="todo"
        android:scaletype="centerinside"
        android:src="@drawable/ic_launcher_background" />

    <textview
        android:id="@+id/tv_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="销售价格"
        android:textsize="14sp" />

</linearlayout>



recyclerviewadapter

package com.jld.exam;

import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.imageview;
import android.widget.textview;

import androidx.annotation.nonnull;
import androidx.recyclerview.widget.recyclerview;

public class recyclerviewadapter extends recyclerview.adapter<recyclerviewadapter.viewholder> {
    private final int[] icno;
    private final string[] desc;

    public recyclerviewadapter(int[] icno, string[] desc) {
        this.icno = icno;
        this.desc = desc;
    }

    @nonnull
    @override
    public viewholder oncreateviewholder(@nonnull viewgroup parent, int viewtype) {
        view view = layoutinflater.from(parent.getcontext()).inflate(r.layout.recyc_item, parent, false);
        return new viewholder(view);
    }

    @override
    public void onbindviewholder(@nonnull viewholder holder, int position) {
        holder.imageview.setimageresource(icno[position]);
        holder.textview.settext(desc[position]);
    }

    @override
    public int getitemcount() {
        return icno.length;
    }

    //viewholder
    public static class viewholder extends recyclerview.viewholder {
        view item;
        imageview imageview;
        textview textview;

        public viewholder(@nonnull view itemview) {
            super(itemview);
            item = itemview;
            imageview = itemview.findviewbyid(r.id.iv_image);
            textview = itemview.findviewbyid(r.id.tv_desc);
        }
    }
}

总结

到此这篇关于android实现简易登陆注册逻辑的文章就介绍到这了,更多相关android登陆注册逻辑内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!