自学内容网 自学内容网

网络资源模板--Android Studio 垃圾分类App

目录

一、项目演示

二、项目测试环境

三、项目详情

四、完整的项目源码 


一、项目演示

网络资源模板--垃圾分类App

二、项目测试环境

三、项目详情

登陆注册

  1. 设置点击监听器:当用户点击注册按钮时触发事件。
  2. 获取用户输入:从输入框获取用户名和密码,并去除前后空格。
  3. 日志记录:使用 Log.i 输出用户名和密码,便于调试。
  4. 创建用户服务实例:实例化 UserService,用于处理注册逻辑。
  5. 创建用户对象:设置用户名和密码到 User 对象。
  6. 调用注册方法:通过 userService.register(user) 进行注册操作。
  7. 显示注册成功提示:使用 Toast 显示注册成功的消息。
  8. 跳转到登录界面:启动 LoginActivity,并关闭当前注册活动。
  this.register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String trim = RegisterActivity.this.username.getText().toString().trim();
                String trim2 = RegisterActivity.this.password.getText().toString().trim();
                Log.i("TAG", trim + "_" + trim2);
                UserService userService = new UserService(RegisterActivity.this);
                User user = new User();
                user.setUsername(trim);
                user.setPassword(trim2);
                userService.register(user);
                Toast.makeText(RegisterActivity.this, "注册成功", 1).show();
                RegisterActivity.this.startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                RegisterActivity.this.finish();
            }
        });

### 功能简单总结

1. **用户输入**:用户可以输入用户名和密码。
2. **记住密码**:用户可以选择“记住密码”,登录时自动填充已保存的账户信息。
3. **登录验证**:点击登录按钮后,应用调用 `UserService` 验证输入的用户名和密码。
4. **登录成功**:
   - 如果验证通过,保存用户的选择(是否记住密码)。
   - 跳转到主界面并结束当前活动。
5. **登录失败**:如果验证失败,显示错误提示。
6. **注册跳转**:用户可以点击注册按钮,跳转到注册页面。

 button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String obj = LoginActivity.this.et_account.getText().toString();
                String obj2 = LoginActivity.this.et_password.getText().toString();
                System.out.println(obj);
                System.out.println(obj2);
                Log.i("TAG", obj + "_" + obj2);
                if (new UserService(LoginActivity.this).login(obj, obj2)) {
                    LoginActivity loginActivity = LoginActivity.this;
                    loginActivity.editor = loginActivity.pref.edit();
                    if (LoginActivity.this.rememberPass.isChecked()) {
                        LoginActivity.this.editor.putBoolean("remember_password", true);
                        LoginActivity.this.editor.putString("account", obj);
                        LoginActivity.this.editor.putString("password", obj2);
                    } else {
                        LoginActivity.this.editor.clear();
                    }
                    LoginActivity.this.editor.apply();
                    Log.i("TAG", "登录成功");
                    Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.putExtra("str", obj);
                    LoginActivity.this.startActivity(intent);
                    LoginActivity.this.finish();
                    return;
                }
                Log.i("TAG", "登录失败");
                Toast.makeText(LoginActivity.this, "密码或者用户名输入错误!", Toast.LENGTH_SHORT).show();
            }
        });

首页

### 功能简单总结

1. **界面初始化**:
   - 加载主界面布局并初始化视图组件(如 `SearchView` 和多个 `ImageView`)。

2. **获取传递数据**:
   - 从 Intent 获取字符串参数,以便在活动间传递。

3. **考试功能**:
   - 点击 `exam` 按钮启动 `ExamActivity`,并传递获取的字符串参数。

4. **搜索功能**:
   - 使用 `SearchView` 进行搜索:
     - **提交搜索**:如果输入不为空,启动 `SearchActivity`,传递搜索内容。
     - **输入变化**:不在输入变化时提示用户。

5. **知识库功能**:
   - 点击 `knowledge` 按钮启动 `KnowledgeActivity`,传递字符串参数。

6. **设置功能**:
   - 点击 `settings` 按钮启动 `SettingsActivity`,传递字符串参数。 

  this.search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            public boolean onQueryTextSubmit(String str) {
                if (TextUtils.isEmpty(str)) {
                    Toast.makeText(MainActivity.this, "请输入查找内容!", Toast.LENGTH_SHORT).show();
                    return true;
                }
                Intent intent = new Intent(MainActivity.this, SearchActivity.class);
                intent.putExtra("garbageID", str);
                intent.putExtra("str", stringExtra);
                MainActivity.this.startActivity(intent);
                MainActivity.this.finish();
                return true;
            }

            public boolean onQueryTextChange(String str) {
                if (!TextUtils.isEmpty(str)) {
                    return true;
                }
                Toast.makeText(MainActivity.this, "请输入查找内容!", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

百科页面

展示对应的四个垃圾分类

设置页面

### 功能简单总结

1. **界面初始化**:
   - 加载设置界面布局并初始化视图组件(如 `LinearLayout` 和 `ImageView`)。

2. **强制离线功能**:
   - 点击 **“强制离线”** 按钮发送广播以切换到离线模式。

3. **用户名显示**:
   - 从 Intent 获取用户名并在界面上显示。

4. **版本信息提示**:
   - 点击 **“版本信息”** 按钮时,显示当前版本提示。

5. **用户协议**:
   - 点击 **“用户协议”** 按钮,启动 `AgreementActivity` 并传递用户名。

6. **评价功能**:
   - 点击 **“评价”** 按钮时,显示感谢提示。

7. **主页导航**:
   - 点击 **“主页”** 按钮,启动 `MainActivity` 并传递用户名。

8. **知识库导航**:
   - 点击 **“知识”** 按钮,启动 `KnowledgeActivity` 并传递用户名。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="160.0dip"
            android:background="@color/white"
            android:scaleType="fitXY"
            android:src="@drawable/setback" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="0.5"
            android:background="@drawable/setbar_bg"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="67.0dip"
                android:layout_height="50.0dip"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:padding="5.0dip"
                android:src="@drawable/username" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center_vertical"
                android:text="当前用户:"
                android:textSize="25.0dip" />

            <TextView
                android:id="@id/username"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center_vertical"
                android:textColor="#ff119233"
                android:textSize="25.0dip" />
        </LinearLayout>

        <LinearLayout
        android:id="@+id/agreement"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="0.5"
            android:background="@drawable/setbar_bg"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="67.0dip"
                android:layout_height="50.0dip"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:padding="5.0dip"
                android:src="@drawable/useragreement" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center_vertical"
                android:text="用户协议"
                android:textSize="25.0dip" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/edition"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="0.5"
            android:background="@drawable/setbar_bg"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="67.0dip"
                android:layout_height="50.0dip"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:padding="5.0dip"
                android:src="@drawable/edition" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center_vertical"
                android:text="当前版本"
                android:textSize="25.0dip" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/evaluate"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="0.5"
            android:background="@drawable/setbar_bg"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="67.0dip"
                android:layout_height="50.0dip"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:padding="5.0dip"
                android:src="@drawable/evaluate" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center_vertical"
                android:text="给个好评!"
                android:textSize="25.0dip" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ForceOffine"
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="0.5"
            android:background="@drawable/setbar_bg"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="67.0dip"
                android:layout_height="50.0dip"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:padding="5.0dip"
                android:src="@drawable/offine" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:gravity="center_vertical"
                android:text="强制下线"
                android:textSize="25.0dip" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="1.0"
            android:orientation="horizontal" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="2.0dip"
            android:background="@color/black" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0.0dip"
            android:layout_weight="0.5"
            android:orientation="horizontal"
            android:paddingTop="5.0dip">

            <LinearLayout
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:layout_gravity="bottom"
                android:layout_weight="1.0"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <ImageView
                    android:id="@id/Main"
                    android:layout_width="40.0dip"
                    android:layout_height="30.0dip"
                    android:gravity="center_vertical"
                    android:src="@drawable/home_light" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:text="首页" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1.0"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <ImageView
                    android:id="@id/baike"
                    android:layout_width="40.0dip"
                    android:layout_height="30.0dip"
                    android:src="@drawable/knowledge" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="百科" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0.0dip"
                android:layout_height="fill_parent"
                android:layout_weight="1.0"
                android:gravity="center_horizontal"
                android:orientation="vertical">

                <ImageView
                    android:id="@id/settings"
                    android:layout_width="40.0dip"
                    android:layout_height="30.0dip"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:src="@drawable/settings" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="设置" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

搜索页面

### 功能简单总结

1. **界面初始化**:
   - 加载搜索界面布局并初始化视图组件(如 `ScrollView` 和 `LinearLayout`)。

2. **返回主页**:
   - 点击返回按钮时,启动 `MainActivity` 并传递用户信息。

3. **获取垃圾信息**:
   - 从 Intent 获取垃圾 ID,调用 API 请求垃圾分类信息。

4. **API 请求**:
   - 使用 `HttpUtil` 发送网络请求,获取垃圾分类数据。
   - 处理请求结果:
     - 如果成功,存储数据到 `SharedPreferences` 并展示信息。
     - 如果失败,显示错误提示。

5. **展示垃圾信息**:
   - 清空当前显示的垃圾信息,并从 API 响应中解析出垃圾数据。
   - 动态创建视图,将垃圾名称、类型、解释、样例和注意事项显示在界面上。

6. **错误处理**:
   - 如果未找到垃圾信息或请求失败,给出相应的提示。

 private void showGarbageInfo(GarbageSearch garbageSearch) {
        this.newslayout.removeAllViews();
        // 确保访问的是 result.list
        if (garbageSearch.result.Newslist == null || garbageSearch.result.Newslist.isEmpty()) {
            Toast.makeText(this, "没有找到垃圾信息", Toast.LENGTH_SHORT).show();
            return;
        }
        for (GarbageGSON.News news : garbageSearch.result.Newslist) {
            View inflate = LayoutInflater.from(this).inflate(R.layout.news_items, (ViewGroup) this.newslayout, false);
            TextView textView = (TextView) inflate.findViewById(R.id.type);
            TextView textView2 = (TextView) inflate.findViewById(R.id.explain);
            TextView textView3 = (TextView) inflate.findViewById(R.id.contain);
            TextView textView4 = (TextView) inflate.findViewById(R.id.tip);
            textView.setText("垃圾名称: " + news.name); // 更新为 news.name
            if (news.type == 0) {
                textView.setText("垃圾类型: 可回收垃圾");
            } else if (news.type == 1) {
                textView.setText("垃圾类型: 不回收垃圾");
            } else if (news.type == 2) {
                textView.setText("垃圾类型: 湿垃圾(厨余垃圾)");
            } else if (news.type == 3) {
                textView.setText("垃圾类型: 干垃圾");
            }
            textView2.setText("类型解释: " + news.explain);
            textView3.setText("类型样例: " + news.contain);
            textView4.setText("分类注意: " + news.tip + "\n");
            this.newslayout.addView(inflate);
        }
        this.garbageLayout.setVisibility(View.VISIBLE);
    }

测验页面

### 功能简单总结

1. **界面初始化**:
   - 加载考试界面布局并初始化视图组件(如返回按钮、题目文本和单选按钮组)。

2. **返回主页**:
   - 点击返回按钮时,启动 `MainActivity` 并传递用户信息。

3. **问题和答案初始化**:
   - 使用 `initializeQuestions()` 方法填充问题和答案列表。

4. **随机加载问题**:
   - 在 `loadNewQuestion()` 方法中生成随机数,加载新的垃圾分类问题并更新界面。

5. **答案选择与提交**:
   - 用户选择答案后,点击提交按钮:
     - 检查是否选择了答案,若未选择则提示用户。
     - 获取用户选择的答案并与当前问题的正确答案进行比较。
     - 显示答题结果(正确与否)。

6. **清空选择与加载新问题**:
   - 在每次提交后清空选择并加载新的问题。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/light_green"
    android:orientation="vertical">

    <androidx.core.widget.NestedScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="80.0dip"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/iv_back"
                    android:layout_width="30.0dip"
                    android:layout_height="40.0dip"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="18.0dip"
                    android:padding="3.0dip"
                    android:src="@mipmap/back_black" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="每日一测"
                    android:textColor="#ffb1102d"
                    android:textSize="35.0dip" />
            </RelativeLayout>

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10.0dip"
                android:text="每道题都只有一个正确选项"
                android:textSize="18.0sp" />

            <TextView
                android:id="@+id/tv_timu"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10.0dip"
                android:text=""
                android:textColor="#ff333333"
                android:textSize="25.0sp" />

            <RadioGroup
                android:id="@+id/select1_1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <RadioButton
                    android:id="@+id/select1_1_1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="10.0dip"
                    android:text="A  可回收物"
                    android:textColor="#ff3e79ef"
                    android:textSize="20.0sp" />

                <RadioButton
                    android:id="@+id/select1_1_2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="10.0dip"
                    android:text="B  干垃圾"
                    android:textColor="#ff1d2a44"
                    android:textSize="20.0sp" />

                <RadioButton
                    android:id="@+id/select1_1_3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="10.0dip"
                    android:text="C  有害垃圾"
                    android:textColor="#ff8b0a50"
                    android:textSize="20.0sp" />

                <RadioButton
                    android:id="@+id/select1_1_4"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:padding="10.0dip"
                    android:text="D  湿垃圾"
                    android:textColor="#ff91ef8a"
                    android:textSize="20.0sp" />
            </RadioGroup>

            <TextView
                android:id="@+id/btn_sub1"
                android:layout_width="150.0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="20.0dip"
                android:background="@drawable/setbar_bg"
                android:paddingLeft="20.0dip"
                android:paddingTop="10.0dip"
                android:paddingRight="20.0dip"
                android:paddingBottom="10.0dip"
                android:text="提交答案"
                android:textColor="@color/black"
                android:textSize="20.0sp" />

            <TextView
                android:id="@+id/answer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20.0dip"
                android:layout_marginTop="20.0dip" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="120.0dip"
        android:scaleType="fitXY"
        android:src="@mipmap/banner2" />
</LinearLayout>

四、完整的项目源码 

👇👇👇👇👇快捷获取方式👇👇👇👇👇


原文地址:https://blog.csdn.net/qq_29823791/article/details/142456127

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!