【学Android开发总结:垃圾分类APP(一)】

登录界面

一、效果图

在这里插入图片描述

二、layout中的代码

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

    <!--照片命名不能有大写-->
    <ImageView
        android:layout_width="150sp"
        android:layout_height="150sp"
        android:src="@drawable/rclogo"
        android:scaleType="fitCenter"
        android:layout_gravity="center"
        android:layout_marginTop="60dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30sp"
        android:layout_marginLeft="20dp"
        android:text="用户名"
        android:textSize="20sp"
        android:layout_marginTop="20dp"/>
    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/shape"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:digits="0123456789"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="30sp"
        android:text="密码"
        android:layout_marginLeft="25sp"
        android:layout_marginTop="10sp"
        android:textSize="20sp" />
    <EditText
        android:id="@+id/et_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:background="@drawable/shape"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:password="true" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <CheckBox
            android:id="@+id/cb_remeberpwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="20dp"
            android:text="记住密码"
            android:layout_gravity="center" />
        <CheckBox
            android:id="@+id/cb_autologin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="自动登录"
            android:layout_gravity="center" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/bt_register"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="注册" />
        <Button
            android:id="@+id/bt_login"
            android:layout_width="0sp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="登录" />
    </LinearLayout>
</LinearLayout>

三、代码补充说明

1、其中可以网上找图片,然后重命名,切记不能有大写字母和数字!!! 然后可以通过复制粘贴到 drawable文件夹里面。
2、输入用户名的文本框美化成图片效果,也就是"@drawable/shape"的作用,就是在 drawable 文件新建一个 shape.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#CCCCCC"/>
    <stroke android:width="1dip" android:color="#fefefe"/>
    <corners android:radius="20dp"/>
    <padding android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp"/>
</shape>

注册界面

一、效果图

在这里插入图片描述

二、layout中的代码

<?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:layout_height="match_parent"
    tools:context=".Register"
    android:orientation="vertical"
    android:background="@drawable/resbackground">
    <TextView
        android:layout_marginTop="80sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="20sp"
        android:text="欢迎加入守护地球的大家庭!"
        android:textColor="#229900"
        android:textSize="25sp"
        android:gravity="center"
        android:textStyle="bold"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_marginTop="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="用户名"
            android:gravity="center"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/shape"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:digits="0123456789"/>
        <TextView
            android:layout_marginTop="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="密码"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:background="@drawable/shape"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:password="true" />
        <TextView
            android:layout_marginTop="20sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="确认密码"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/password1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:background="@drawable/shape"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginBottom="15dp"
            android:password="true" />
        <Button
            android:id="@+id/bt_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20sp"
            android:textSize="25sp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:text="注册"
            />
    </LinearLayout>
</LinearLayout>

设置登录页面为进程序的页面

在这里插入图片描述
红圈中的代码放在哪个 activity 中就表示进入该软件的首个页面为哪个,所以我们需要把这个代码剪切到 login的activity中

加入启动程序时的加载图片

一、在 values 文件夹中的 themes.xml 中加入以下代码

<!--第一个为名字,第二个为项目名字-->
<style name="Theme.Response" parent="Theme.Goodrubish">
        <!--图片路径-->
        <item name="android:windowBackground">@drawable/responseinterface</item>
        <!--设置全屏-->
        <item name="android:windowFullscreen">true</item>
        <!--设置透明隐藏顶部栏-->
        <item name="android:navigationBarColor">@android:color/transparent</item>
    </style>

二、去 AndroidManifest.xml 中加入一行代码

 <activity
            android:name=".login"
            android:exported="true"
            <!--就是下面这行代码,名字上面取的-->
            android:theme="@style/Theme.Response">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </activity>

三、在登录的Java代码中加一行,才能使图片在启动程序完消失

protected void onCreate(Bundle savedInstanceState) {
        //加入这行代码加载最开始我们设定的界面
        setTheme(R.style.Theme_Goodrubish);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }

注册的Java代码

package com.example.goodrubish;

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

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

public class Register extends AppCompatActivity {
    private Button bt_register;
    private EditText user,password,password1;
    private  String userName,pwd,pwdAgain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();  //初始化

        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()){
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void initView() {
        //找到布局中的各个控件
        bt_register = findViewById(R.id.bt_register);
        user = findViewById(R.id.user);
        password = findViewById(R.id.password);
        password1 = findViewById(R.id.password1);
        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //读取用户的输入数据
                userName = user.getText().toString().trim();
                pwd = password.getText().toString().trim();
                pwdAgain = password1.getText().toString().trim();

                if(TextUtils.isEmpty(userName)){
                    Toast.makeText(Register.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(pwd)){
                    Toast.makeText(Register.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(pwdAgain)){
                    Toast.makeText(Register.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(!pwd.equals(pwdAgain)){
                    Toast.makeText(Register.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                    return;
                    /**
                     *从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
                     */
                }else if(isExistUserName(userName)){
                    Toast.makeText(Register.this, "此账户名已经存在", Toast.LENGTH_SHORT).show();
                    return;
                }else{
                    Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                    //把账号、密码和账号标识保存到sp里面
                    /**
                     * 保存账号和密码到SharedPreferences中
                     */
                    saveRegisterInfo(userName, pwd);
                    //注册成功后把账号传递到login.java中
                    // 返回值到loginActivity显示
                    Intent data = new Intent();
                    data.putExtra("userName", userName);
                    setResult(RESULT_OK, data);
                    //RESULT_OK为Activity系统常量,状态码为-1,
                    // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值
                    Register.this.finish();
                }
            }
        });
    }
    private void saveRegisterInfo(String userName, String pwd) {
        String md5Psw = MD5Utils.md5(pwd);//把密码用MD5加密
        //loginInfo表示文件名, mode_private SharedPreferences sp = getSharedPreferences( );
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        //获取编辑器, SharedPreferences.Editor  editor -> sp.edit();
        SharedPreferences.Editor editor=sp.edit();
        //以用户名为key,密码为value保存在SharedPreferences中
        //key,value,如键值对,editor.putString(用户名,密码);
        editor.putString(userName, md5Psw);
        //提交修改 editor.commit();
        editor.commit();
    }
    private boolean isExistUserName(String userName) {
        boolean has_userName=false;
        //mode_private SharedPreferences sp = getSharedPreferences( );
        // "loginInfo", MODE_PRIVATE
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        //获取密码
        String spPsw=sp.getString(userName, "");//传入用户名获取密码
        //如果密码不为空则确实保存过这个用户名
        if(!TextUtils.isEmpty(spPsw)) {
            has_userName=true;
        }
        return has_userName;
    }
}

使用MD5加密密码

新建一个 MD5Utils

package com.example.goodrubish;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    public static String md5(String text) {
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("md5");
            // 数组 byte[] result -> digest.digest( );  文本 text.getBytes();
            byte[] result = digest.digest(text.getBytes());
            //创建StringBuilder对象 然后建议StringBuffer,安全性高
            //StringBuilder sb = new StringBuilder();
            StringBuffer sb = new StringBuffer();
            // result数组,digest.digest ( ); -> text.getBytes();
            // for 循环数组byte[] result;
            for (byte b : result) {
                int number = b & 0xff;
                String hex = Integer.toHexString(number);
                if (hex.length() == 1) {
                    sb.append("0" + hex);
                } else {
                    sb.append(hex);
                }
            }
            //sb StringBuffer sb = new StringBuffer();对象实例化
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            //发送异常return空字符串
            return "";
        }
    }
}

登录的Java代码

package com.example.goodrubish;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
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.Toast;

public class login extends AppCompatActivity {

    SharedPreferences sp;

    private EditText et_name;
    private EditText et_pwd;
    private CheckBox cb_remeberpwd;
    private CheckBox cb_autologin;
    private Button bt_register;
    private Button bt_login;
    //用户名,密码,加密密码
    private String userName, password, spPsw;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Goodrubish);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //获取首选项 sp
        sp = getSharedPreferences("config", Context.MODE_PRIVATE);
        //初始化
        initView();
        //第二次打开的时候,从sp获取数据,进行画面的同步
        boolean rememberpwd = sp.getBoolean("remeberpwd", false); //获取为空则为false
        boolean autologin = sp.getBoolean("autologin", false);

        //记住密码
        if (rememberpwd) {
            //获取sp里的 name 和 pwd 并且保存到 EditText 上
            String pwd = sp.getString("pwd", null);
            et_pwd.setText(pwd);
            String name = sp.getString("name", null);
            et_name.setText(name);
            //记住密码打钩
            cb_remeberpwd.setChecked(true);
        }

        //自动登录
        if (autologin) {
            cb_autologin.setChecked(true);
            String pwd = sp.getString("pwd", null);
            et_pwd.setText(pwd);
            String name = sp.getString("name", null);
            et_name.setText(name);
            //自动登录
            startActivity(new Intent(login.this, MainActivity.class));
        }
    }

    private void initView() {
        //找到控件
        et_name = findViewById(R.id.et_name);
        et_pwd = findViewById(R.id.et_pwd);
        cb_remeberpwd = findViewById(R.id.cb_remeberpwd);
        cb_autologin = findViewById(R.id.cb_autologin);
        bt_register = findViewById(R.id.bt_register);
        bt_login = findViewById(R.id.bt_login);

        //设置监听
        MyOnClickListener l = new MyOnClickListener();
        bt_login.setOnClickListener(l);
        bt_register.setOnClickListener(l);
    }

    private class MyOnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_register:
                    //跳转到主界面,登录成功的状态传递到 MainActivity 中
                    startActivity(new Intent(login.this, Register.class));
                    break;

                case R.id.bt_login:
                    //登录操作
                    //获取账号和密码
                    String name = et_name.getText().toString().trim();
                    String pwd = et_pwd.getText().toString().trim();
                    if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
                        Toast.makeText(login.this, "用户名或者密码为空", Toast.LENGTH_SHORT).show();
                    } else {
                        // md5Psw ; spPsw 为 根据从SharedPreferences中用户名读取密码
                        // 定义方法 readPsw为了读取用户名,得到密码
                        String md5Psw = MD5Utils.md5(pwd);
                        spPsw = readPsw(name);
                        //对当前用户输入的密码进行MD5加密再进行比对判断, MD5Utils.md5( ); psw 进行加密判断是否一致
                        if (md5Psw.equals(spPsw)) {
                            //记住密码打钩没有
                            if (cb_remeberpwd.isChecked()) {
                                //sp保存账号和密码 还有记住密码的状态
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("name", name);
                                editor.putString("pwd", pwd);
                                editor.putBoolean("remeberpwd", true);
                                editor.apply();
                            }
                            if (cb_autologin.isChecked()) {
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putBoolean("autologin", true);
                                editor.apply();
                            }

                            Toast.makeText(login.this, "登录成功", Toast.LENGTH_SHORT).show();
                            //保存登录状态,在界面保存登录的用户名 定义个方法 saveLoginStatus boolean 状态 , userName 用户名;
                            saveLoginStatus(true, name);
                            //登录成功后关闭此页面进入主页
                            Intent data = new Intent();
                            //datad.putExtra( ); name , value ;
                            data.putExtra("isLogin", true);
                            //RESULT_OK为Activity系统常量,状态码为-1
                            // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值
                            setResult(RESULT_OK, data);
                            //销毁登录界面
                            login.this.finish();
                            //跳转到主界面,登录成功的状态传递到 MainActivity 中
                            startActivity(new Intent(login.this, MainActivity.class));
                            return;
                        } else {
                            Toast.makeText(login.this, "用户名和密码不一致", Toast.LENGTH_SHORT).show();
                        }
                    }
                    break;
            }
        }
    }

    private void saveLoginStatus(boolean status, String name) {
        //saveLoginStatus(true, userName);
        //loginInfo表示文件名  SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        //获取编辑器
        SharedPreferences.Editor editor = sp.edit();
        //存入boolean类型的登录状态
        editor.putBoolean("isLogin", status);
        //存入登录状态时的用户名
        editor.putString("loginUserName", userName);
        //提交修改
        editor.commit();
    }

    private String readPsw(String name) {
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.getString(name, "");
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            //是获取注册界面回传过来的用户名
            // getExtra().getString("***");
            String name = data.getStringExtra("name");
            if (!TextUtils.isEmpty(name)) {
                //设置用户名到 et_name 控件
                et_name.setText(name);
                //et_name控件的setSelection()方法来设置光标位置
                et_name.setSelection(name.length());
            }
        }
    }
}

第一步算是完成了,好难!!!

用了MD5对账号加密码进行加密然后保存,所以直接根据账号判断密码是否正确,然后用了sp保存,没有用数据库,大家可以自行了解用数据库的方法!!!

我的其他文章

垃圾分类APP(一)

垃圾分类APP(二)

垃圾分类APP(三)

垃圾分类 APP(四)

垃圾分类APP(五)

  • 0
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
安卓期末项目“垃圾分类助手”是一个基于Android Studio开发的应用程序。该应用程序提供了垃圾分类相关的功能和服务。在这个项目中,使用了一些核心类和组件,如Base Adapter,Fragment,View Pager和Alert Dialog等。 该分类助手的功能包括管理员登录后台界面,将数据录入数据库,并进行增删改查操作。此外,用户可以在前台页面通过垃圾分类查找垃圾,也可以通过垃圾查找分类。同时,用户还可以浏览后台管理员录入数据库的新闻。 该项目在上传时可能包含了压缩包,其中包含了源代码、项目文档、apk文件以及运行各个界面的截图。这些文件可以帮助理解和使用该应用程序。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [安卓期末大作业(AndroidStudio开发),垃圾分类助手app【附13项目说明文档】](https://download.csdn.net/download/qq_40957277/87907677)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [安卓期末大作业-垃圾分类助手-(AndroidStudio开发)-(含课程报告)](https://download.csdn.net/download/qq_33037637/88252608)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.bei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值