zl程序教程

您现在的位置是:首页 >  其他

当前栏目

登陆和注册,两个Activity和一个XML文件

文件注册XML 一个 两个 登陆 Activity
2023-09-14 09:04:30 时间

XML文件

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="请输入账号"
    android:id="@+id/edittext1"
    android:textSize="20sp" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请输入密码"
        android:id="@+id/edittext2"
        android:textSize="20sp"
        android:password="true"
        android:layout_marginTop="10dp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆"
        android:id="@+id/button"
        android:textSize="25sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:textSize="25sp"
        android:id="@+id/zhuce"
        android:gravity="right"/>
</LinearLayout>

主界面:

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;



public class MainActivity extends AppCompatActivity {
        EditText edittext1,edittext2;//控件声明
        Button button;
        TextView zhuce;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Key_things();

        edittext1=(EditText)findViewById(R.id.edittext1);//绑定ID
        edittext2=(EditText)findViewById(R.id.edittext2);
        button=(Button)findViewById(R.id.button);
        zhuce=(TextView)findViewById(R.id.zhuce);
    }

    private void Key_things() {

                button=(Button)findViewById(R.id.button);
                button.setOnClickListener(new View.OnClickListener() {//监听按键
                    @Override
                    public void onClick(View v) {
                        String Str_Name = edittext1.getText().toString();//把输入的值取出来,赋值方便登陆判断的时候使用,不然要用那么大串数据去比较,就很复杂
                        String Str_password = edittext2.getText().toString();

                //从ShardPrenfence获取值并进行比对
                SharedPreferences spf = getSharedPreferences("myspf",MODE_PRIVATE);//创建一个注册时使用过的小型数据库
                String Spf_Get_Name = spf.getString("spf_name",null);//因为注册的时候已经把值保存在小型数据库里面的了,现在获取出来,用于登陆判断
                String Spf_Get_Password = spf.getString("spf_password",null);

                //进行比对
                /*
                 * 1.先判断用户名有没有填写,为空要用toast提示
                 * 2.判断密码有没有填写,为空要用toast提示
                 * 3.判断用户名是否匹配,不匹配要用toast提示无该用户
                 * 4.判断密码是否匹配,不匹配要用toast提示密码错误,匹配成功则成功登陆
                 * */
                if (Str_Name ==null ||Str_Name.length() == 0){
                    Toast.makeText(MainActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                }else {
                    if (Str_password == null || Str_password.length() ==0){
                        Toast.makeText(MainActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    }else {
                        if (Str_Name.equals(Spf_Get_Name)){
                            if (Str_password.equals(Spf_Get_Password)){
                                Toast.makeText(MainActivity.this, "验证成功!!", Toast.LENGTH_LONG).show();
                            }else {
                                Toast.makeText(MainActivity.this, "密码不正确", Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            Toast.makeText(MainActivity.this, "用户名不存在", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });



        zhuce=(TextView)findViewById(R.id.zhuce);
    zhuce.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {//Intent函数用来跳转Activity,跳到注册的Activity,不过用的界面还是登陆界面,改了一点点而已
        Intent intent=new Intent();
        intent.setClass(MainActivity.this,zhuce.class);
        startActivity(intent);
    }
});

}




}




第二个Activity:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class zhuce extends AppCompatActivity {
    EditText ET_Name,ET_Password;//控件声明
    Button BTN_Zhuce;
    TextView TX_Zhuce;

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

        find_id();//函数声明
        Key_things();
        BTN_Zhuce.setText("注册");//在这个activity,“登陆”按键显示为注册(改了一个名字)
        TX_Zhuce.setVisibility(View.GONE);//而且右下角的那个注册去掉了
    }

    private void Key_things() {//如果要创建函数,在类里面创,在onCreate函数外。如果创建类,要在最大的那个括号外面创。如果不创,就在onCreate里面创
        BTN_Zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences spf = getSharedPreferences("myspf",MODE_PRIVATE);//获得SharedPreferences对象
                SharedPreferences.Editor editor = spf.edit();//然后再在SharedPreferences对象的基础上,使用SharedPreferences接口的edit获得SharedPreferences.Editor对象:editor(因为需要SharedPreferences.Editor才可以编写数据库)
                editor.putString("spf_name",ET_Name.getText().toString());//注册的时候是往小数据库里面存数据,即PutString,登陆的时候是取出来作比较,即GetString
                editor.putString("spf_password",ET_Password.getText().toString());//(关键字,内容)
                editor.commit();//提交完成
                finish();
            }
        });
    }

    private void find_id() {//控件绑定
        ET_Name = findViewById(R.id.edittext1);
        ET_Password = findViewById(R.id.edittext2);
        BTN_Zhuce = findViewById(R.id.button);
        TX_Zhuce = findViewById(R.id.zhuce);
    }
}

在这里插入图片描述