网络资源模板--基于 Android Studio 实现的新闻App

目录

一、项目测试视频

二、项目简介

三、项目测试环境

四、项目详情设计

五、源码获取

原创 新闻 项目 :Android Studio 实现网易新闻App (简单方便易懂)

一、项目测试视频

网络资源模板--基于Android studio 实现新闻App 

二、项目简介

该新闻APP使用了MySQL数据库进行账号的登录注册以及修改个人信息。
还使用了API进行新闻的展示以及刷新新闻功能!

三、项目测试环境

四、项目详情设计

<?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"
    android:orientation="vertical"
    tools:context=".menu.AboutActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/news"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="爱看APP"
            android:textStyle="bold"
            android:textColor="#000"
            android:textSize="25sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="版本"
            android:textColor="#000"
            android:textStyle="bold"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.0.0"
            android:textSize="18sp"
            android:textStyle="bold"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="#DFD6D6"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="作者"
            android:textColor="#000"
            android:textStyle="bold"
            android:textSize="20sp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xxx"
            android:textSize="18sp"
            android:textStyle="bold" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="#DFD6D6"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开发者自语"
            android:textColor="#000"
            android:textStyle="bold"
            android:textSize="20sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="        随着大数据时代的到来,传统媒体新闻传播效能不断降低;而新兴媒体的崛起,使人们的生活和阅读方式不再单一,但也以大量的文字为主,不适合当代的碎片化阅读习惯,本项目致力于让新闻转变为图像与简短文字的结合,采用类似抖音快手瀑布流的图集滚动页面,能更加符合当代年轻人碎片化阅读的习惯。"
            android:textSize="18sp"
            android:textStyle="bold"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="10dp"
            android:background="#DFD6D6"/>
    </LinearLayout>

   

</LinearLayout>

这段代码实现了一个用户注册功能的 `RegisterActivity`,用户可以填写账号、密码和用户名进行注册,注册结果将通过 `Handler` 处理并显示相应的 Toast 提示。以下是代码的详细功能和流程:

1. **布局与初始化**:
   - 在 `onCreate` 方法中,设置了布局文件 `activity_register`,并初始化了界面上的 `EditText` 控件 `userAccount`、`userPassword` 和 `userName`,用于用户输入账号、密码和用户名。

2. **注册逻辑**:
   - `register` 方法通过点击注册按钮触发,获取用户输入的账号、密码和用户名。
   - 创建一个 `User` 对象,将获取的用户信息设置到 `User` 对象中。
   - 新开一个线程处理注册逻辑:
     - 使用 `UserDao` 类处理数据库操作。
     - 调用 `findUser` 方法检查是否已存在相同账号。
       - 如果存在相同账号,则将消息设置为 `1`,表示注册失败并提示账号已存在。
     - 如果账号不存在,则调用 `register` 方法进行用户注册。
       - 如果注册成功,将消息设置为 `2`,表示注册成功。
   - 使用 `Handler` 的 `sendEmptyMessage` 方法发送处理结果消息。

3. **消息处理**:
   - `Handler` 对象 `hand` 通过重写 `handleMessage` 方法处理消息。
   - 根据接收到的消息类型(0、1、2),显示相应的 Toast 提示:
     - `msg.what == 0`:注册失败。
     - `msg.what == 1`:账号已存在,注册失败。
     - `msg.what == 2`:注册成功,显示成功提示,并设置返回结果给调用者。

4. **返回结果**:
   - 在注册成功时,创建一个 `Intent` 对象,并通过 `putExtra` 方法封装要传递的数据。
   - 使用 `setResult` 方法设置注册结果为 `RESULT_CANCELED`,并将 `Intent` 对象传递回给调用者。
   - 调用 `finish()` 方法结束当前 `RegisterActivity`。

这段代码通过使用 `Handler` 处理异步消息,实现了用户注册的功能,并在注册成功或失败时通过 Toast 提示用户相应的信息,同时将注册结果传递给调用者。

package com.example.news;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.news.dao.UserDao;
import com.example.news.entity.User;

public class RegisterActivity extends AppCompatActivity {
    private static final String TAG = "mysql-party-register";
    EditText userAccount = null;
    EditText userPassword = null;
    EditText userName = null;


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

        userAccount = findViewById(R.id.userAccount);
        userPassword = findViewById(R.id.userPassword);
        userName = findViewById(R.id.userName);

    }

    public void register(View view) {
        String userAccount1 = userAccount.getText().toString();
        String userPassword1 = userPassword.getText().toString();
        String userName1 = userName.getText().toString();
        User user = new User();
        user.setUserAccount(userAccount1);
        user.setUserPassword(userPassword1);
        user.setUserName(userName1);


        new Thread() {
            @Override
            public void run() {
                int msg = 0;
                UserDao userDao = new UserDao();
                User uu = userDao.findUser(user.getUserAccount());
                if (uu != null) {
                    msg = 1;
                } else {
                    boolean flag = userDao.register(user);
                    if (flag) {
                        msg = 2;
                    }
                }
                hand.sendEmptyMessage(msg);
            }
        }.start();


    }

    @SuppressLint("HandlerLeak")
    final Handler hand = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_LONG).show();
            } else if (msg.what == 1) {
                Toast.makeText(getApplicationContext(), "该账号已经存在,请换一个账号", Toast.LENGTH_LONG).show();
            } else if (msg.what == 2) {
                Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                //将想要传递的数据用putExtra封装在intent中
                intent.putExtra("a", "注册");
                setResult(RESULT_CANCELED, intent);
                finish();
            }
        }
    };

}

这段代码实现了一个简单的用户登录功能的 `LoginActivity`。用户可以输入账号和密码进行登录,登录结果通过 `Handler` 处理并显示相应的 Toast 提示,并根据不同的结果跳转到不同的页面。

以下是代码的详细功能和流程:

1. **布局与初始化**:
   - 在 `onCreate` 方法中,设置了布局文件 `activity_login`,并初始化了界面上的 `EditText` 控件 `EditTextAccount` 和 `EditTextPassword`,用于用户输入账号和密码。

2. **跳转到注册页面**:
   - `reg` 方法通过点击注册按钮触发,使用 `Intent` 跳转至 `RegisterActivity` 页面,实现用户注册功能。

3. **登录逻辑**:
   - `login` 方法通过点击登录按钮触发,新开一个线程处理登录逻辑。
   - 在新线程中,创建 `UserDao` 对象处理数据库操作,调用 `login` 方法验证用户输入的账号和密码。
   - `login` 方法返回一个 `int` 类型的消息码 `msg`,用来表示不同的登录结果:
     - `msg == 0`:登录失败,未知错误。
     - `msg == 1`:登录成功,跳转至 `MainActivity` 页面,并将账号信息通过 `Intent` 的 `putExtra` 方法传递。
     - `msg == 2`:密码错误,提示用户重新输入。
     - `msg == 3`:账号不存在,提示用户检查账号输入。
   - 使用 `Handler` 的 `sendEmptyMessage` 方法发送处理结果消息。

4. **消息处理**:
   - `Handler` 对象 `hand1` 通过重写 `handleMessage` 方法处理登录结果消息。
   - 根据接收到的消息类型(0、1、2、3),显示相应的 Toast 提示:
     - `msg.what == 0`:登录失败,未知错误。
     - `msg.what == 1`:登录成功,跳转至 `MainActivity` 页面。
     - `msg.what == 2`:密码错误,提示用户重新输入。
     - `msg.what == 3`:账号不存在,提示用户检查账号输入。

这段代码通过多线程处理用户登录逻辑,通过 `Handler` 将结果反馈给主线程处理界面更新和跳转逻辑,实现了基本的用户登录和跳转功能。

package com.example.news;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.news.dao.UserDao;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "mysql-party-MainActivity";
    EditText EditTextAccount;
    EditText EditTextPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        EditTextAccount = findViewById(R.id.uesrAccount);
        EditTextPassword = findViewById(R.id.userPassword);
    }

    /**
     *跳转到注册页面
     */
    public void reg(View view){
        startActivity(new Intent(getApplicationContext(), RegisterActivity.class));
    }


    /**
     * function: 登录
     * */
    public void login(View view){

        new Thread(){
            @Override
            public void run() {
                UserDao userDao = new UserDao();
                int msg = userDao.login(EditTextAccount.getText().toString(),EditTextPassword.getText().toString());
                hand1.sendEmptyMessage(msg);
            }
        }.start();

    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
            } else if (msg.what == 1) {
                Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                intent.putExtra("Account",EditTextAccount.getText().toString());
                startActivity(intent);
                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
            } else if (msg.what == 2){
                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
            } else if (msg.what == 3){
                Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
            }
        }
    };

}

<?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"
    android:orientation="vertical"
    tools:context=".menu.UpdateUserActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="编辑资料"
        android:gravity="center"
        android:textSize="20sp"
        android:background="#2793EA"
        android:textColor="#F4F2F6"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="219dp"
        tools:layout_editor_absoluteY="207dp"
        android:padding="50dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:text="昵称:" />

            <EditText
                android:id="@+id/update_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:text="密码:"
                />

            <EditText
                android:id="@+id/update_password"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:text="性别:"
                />

            <EditText
                android:id="@+id/update_sex"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                />
        </LinearLayout>

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

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:text="年龄:"
                />

            <EditText
                android:id="@+id/update_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="15sp"
                android:text="邮箱:"
                />

            <EditText
                android:id="@+id/update_email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                />
        </LinearLayout>

        <Button
            android:layout_marginTop="50dp"
            android:id="@+id/btn_update"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="保存"
            android:onClick="update"/>

    </LinearLayout>



</LinearLayout>

五、源码获取

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

  • 13
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这个项目听起来很有意思!基于Android Studio实现的在线学习课堂APP,它可以为学生提供一个在线学习的平台,包括在线课程、讨论区和考试等功能。 如果您需要帮助,我可以为您提供一些建议和指导。以下是一些可能会有用的提示: 1. 确定您的需求:在开始编写代码之前,您需要确定您的需求,包括您想要实现哪些功能,用户应该如何使用您的应用程序等。 2. 设计应用程序的界面:使用Android Studio的布局编辑器,您可以为您的应用程序设计用户界面。请记住,用户界面应该易于使用,并且应该符合Android应用程序的设计准则。 3. 实现应用程序的功能:一旦您设计好了用户界面,您可以开始实现应用程序的功能。您可以使用Java编写代码,并使用Android Studio提供的各种工具和API来实现您的功能。 4. 测试您的应用程序:在编写完代码后,您需要测试您的应用程序,以确保它能够按照您的预期运行,并且没有任何错误或缺陷。您可以使用Android Studio提供的模拟器或在实际设备上测试您的应用程序。 5. 发布您的应用程序:最后,您需要发布您的应用程序。您可以将应用程序上传到Google Play商店或其他应用程序市场,以便用户下载和使用。 希望这些提示可以帮助您开始您的项目!如果您有任何其他问题或需要更多的帮助,请告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

编程乐学

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

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

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

打赏作者

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

抵扣说明:

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

余额充值