Android_登录注册小实例

110 阅读4分钟

首先我对EditView,button,textview外观设置了一下。

目录

简单的外观设置

文字点击设置(变色)

按钮点击设置(变色)

输入框外观设置(好看)

登录页面的布局设置

 注册页面的布局设置

登录成功页面的布局设置 

登录页面的Java逻辑代码

注册页面的逻辑代码

登录成功页面Java代码


简单的外观设置

文字点击设置(变色)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimary" android:state_pressed="true" />
</selector>

按钮点击设置(变色)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/colorPrimary"/>
    <item android:state_pressed="false" android:drawable="@color/colorPrimaryDark"/>
</selector>

输入框外观设置(好看)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/colorPrimary"/>
    <item android:state_pressed="false" android:drawable="@color/colorPrimaryDark"/>
</selector>

登录页面的布局设置

<?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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:layout_margin="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="25dp"
            android:layout_marginRight="5dp"
            />

        <EditText
            android:id="@+id/et_account_one"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="请输入用户名或手机号"
            android:inputType="text"
            style="@style/MyEditStyle" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="25dp"
            android:layout_marginRight="5dp"
            />

        <EditText
            android:id="@+id/et_password_one"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="请输入密码"
            style="@style/MyEditStyle"
            android:inputType="numberPassword"
             />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="20dp">

        <CheckBox
            android:id="@+id/ck_remeber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            android:textSize="20dp"/>

        <CheckBox
            android:id="@+id/ck_Automatic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自动登录"
            android:textSize="20dp"
            android:layout_marginLeft="30dp"/>

    </LinearLayout>

    <Button
        android:id="@+id/btm_signin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:onClick="SignIn"
        style="@style/MybtmStyle" />

    <TextView
        android:id="@+id/tv_register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_gravity="right"
        android:text="没账号?立即注册"
        android:clickable="true"
        android:background="@drawable/text"
        android:textColor="@color/colorPrimary"/>
</LinearLayout>

效果图

 注册页面的布局设置

<?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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:layout_margin="15dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="账  号 :"
            android:textSize="25dp" />

        <EditText
            android:id="@+id/ed_account"
            style="@style/MyEditStyle"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="请输入用户名或手机号"
            android:inputType="text" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:layout_marginTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密  码 :"
            android:textSize="25dp"
            android:layout_marginRight="5dp"
            />

        <EditText
            android:id="@+id/ed_password"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="请输入密码"
            android:inputType="numberPassword"
            style="@style/MyEditStyle" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:layout_marginTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认密码:"
            android:textSize="25dp"
            android:layout_marginRight="5dp"
            />

        <EditText
            android:id="@+id/ed_containPassword"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:hint="再次输入密码"
            android:inputType="numberPassword"
            style="@style/MyEditStyle"/>

    </LinearLayout>


    <Button
        android:id="@+id/btn_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        style="@style/MybtmStyle" />

    <CheckBox
        android:id="@+id/cb_agree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_gravity="left"
        android:text="同意协议"
        android:textColor="@color/colorPrimary"/>
</LinearLayout>

效果图

登录成功页面的布局设置 

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

    <TextView
        android:id="@+id/tv_comein"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录成功"
        android:textSize="35dp"
        android:gravity="center_horizontal"/>

</LinearLayout>

效果图就不贴了,上面这个很简单,然后附上Java代码

登录页面的Java逻辑代码

package com.example.register;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    EditText et_account;
    EditText et_password;
    CheckBox cb_remeber;
    CheckBox cb_Auto;
    Button btn_signin;
    TextView tv_register;
    Map<String,Integer> map=new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_account = findViewById(R.id.et_account_one);
        et_password = findViewById(R.id.et_password_one);
        cb_remeber = findViewById(R.id.ck_remeber);
        cb_Auto = findViewById(R.id.ck_Automatic);
        btn_signin = findViewById(R.id.btm_signin);
        tv_register = findViewById(R.id.tv_register);
        //点击注册按钮
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentRegister = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intentRegister);
            }
        });
    }
    //点击登录按钮
    public void SignIn(View view) {
        //获取SharedPreferences类的对象
        SharedPreferences share = getSharedPreferences("account1", MODE_PRIVATE);
        String ac=share.getString("zhanghao1","");
        int ps = share.getInt("mima1", 0);
        //TextUtils工具类是一个很好用的,判断两个用户名字符串是否相等,或者为空 即返回
        if(!TextUtils.equals(ac,et_account.getText().toString())||
                et_account.getText().toString().isEmpty()){
            Toast.makeText(MainActivity.this,"用户名错误或者为空",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        //判断密码和储存的不一样或者为空即返回
        if(Integer.parseInt(et_password.getText().toString())!=ps ||
                et_password.getText().toString().isEmpty()){
            Toast.makeText(MainActivity.this,"密码错误或者为空",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        //如果都一样即可跳转页面并传入用户名参数给下一个页面
        if (Integer.parseInt(et_password.getText().toString())==ps
                &&TextUtils.equals(ac,et_account.getText().toString())){
            }
            Intent main3 = new Intent(MainActivity.this, MainActivity3.class);
            main3.putExtra("username", et_account.getText().toString());
            startActivity(main3);
            return;
        }
    }

注册页面的逻辑代码

package com.example.register;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity2 extends AppCompatActivity {
    EditText ed_account;
    EditText ed_password;
    EditText ed_contain;
    CheckBox cb_agree;
    Button btn_register;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ed_account = findViewById(R.id.ed_account);
        ed_password = findViewById(R.id.ed_password);
        ed_contain = findViewById(R.id.ed_containPassword);
        cb_agree = findViewById(R.id.cb_agree);
        btn_register = findViewById(R.id.btn_register);
        //点击按钮
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //首先需要判断协议是否勾选
                if(!cb_agree.isChecked()){
                    Toast.makeText(MainActivity2.this, "还未同意协议,无法注册",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                //创建一个SharedPreferences类对象用于储存数据
                SharedPreferences shared = 
                            getSharedPreferences("account1", MODE_PRIVATE);
                //对象中有个方法创建内部类对象,此内部类对象可以放入参数持久化存储
                SharedPreferences.Editor edit = shared.edit();
                //内部类对象用于存,外部类对象用于取,判断账号已经被注册或者为空
                if (!shared.contains("zhanghao1")&&
                        !ed_account.getText().toString().isEmpty()) {
                    edit.putString("zhanghao1",
                                    ed_account.getText().toString());
                } else {
                    Toast.makeText(MainActivity2.this, 
                                "该账号已经被注册或者为空",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                //判断密码不一致或者空
                if(TextUtils.equals(ed_contain.getText().toString(),
                        ed_password.getText().toString())
                        &&!ed_password.getText().toString().isEmpty()                                                                
                        &&!ed_contain.getText().toString().isEmpty()){
                 edit.putInt("mima1",Integer.parseInt    
                                (ed_password.getText().toString()));
                }else{
                    Toast.makeText(MainActivity2.this,
                    "密码不一致或者有一项为空"
                    ,Toast.LENGTH_SHORT).show();
                    return;
                }
                //存进去,没有下面这两条其中之一就无法成功存储
                edit.commit();
                //edit.apply();

                Toast.makeText(MainActivity2.this,"已经保存",Toast.LENGTH_SHORT).show();
                //结束当前页面
                MainActivity2.this.finish();
            }
        });
    }
}

登录成功页面Java代码

package com.example.register;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity3 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        TextView tx = (TextView) findViewById(R.id.tv_comein);
        Intent intent = getIntent();
        if (intent != null) {
            String name = intent.getStringExtra("username");
            tx.setText(name+"用户欢迎回来");
        }
    }
}