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);
        }
    }
}

총결산
안 드 로 이 드 가 간단 한 로그 인 등록 논 리 를 실현 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 안 드 로 이 드 로그 인 등록 논리 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기