当前位置:   article > 正文

网络资源模板--基于 Android Studio 实现的新闻App_android studio新闻客户端制作

android studio新闻客户端制作

目录

一、项目测试视频

二、项目简介

三、项目测试环境

四、项目详情设计

五、源码获取

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

一、项目测试视频

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

二、项目简介

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

三、项目测试环境

四、项目详情设计

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".menu.AboutActivity">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="200dp"
  12. android:orientation="vertical">
  13. <ImageView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center"
  17. android:src="@drawable/news"/>
  18. <TextView
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_gravity="center_horizontal"
  22. android:text="爱看APP"
  23. android:textStyle="bold"
  24. android:textColor="#000"
  25. android:textSize="25sp"/>
  26. </LinearLayout>
  27. <LinearLayout
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_margin="10dp"
  31. android:orientation="vertical">
  32. <TextView
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="版本"
  36. android:textColor="#000"
  37. android:textStyle="bold"
  38. android:textSize="20sp"/>
  39. <TextView
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="1.0.0"
  43. android:textSize="18sp"
  44. android:textStyle="bold"/>
  45. <View
  46. android:layout_width="match_parent"
  47. android:layout_height="1dp"
  48. android:layout_marginTop="10dp"
  49. android:background="#DFD6D6"/>
  50. </LinearLayout>
  51. <LinearLayout
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:layout_margin="10dp"
  55. android:orientation="vertical">
  56. <TextView
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:text="作者"
  60. android:textColor="#000"
  61. android:textStyle="bold"
  62. android:textSize="20sp"/>
  63. <TextView
  64. android:layout_width="wrap_content"
  65. android:layout_height="wrap_content"
  66. android:text="xxx"
  67. android:textSize="18sp"
  68. android:textStyle="bold" />
  69. <View
  70. android:layout_width="match_parent"
  71. android:layout_height="1dp"
  72. android:layout_marginTop="10dp"
  73. android:background="#DFD6D6"/>
  74. </LinearLayout>
  75. <LinearLayout
  76. android:layout_width="match_parent"
  77. android:layout_height="wrap_content"
  78. android:layout_margin="10dp"
  79. android:orientation="vertical">
  80. <TextView
  81. android:layout_width="wrap_content"
  82. android:layout_height="wrap_content"
  83. android:text="开发者自语"
  84. android:textColor="#000"
  85. android:textStyle="bold"
  86. android:textSize="20sp"/>
  87. <TextView
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content"
  90. android:text=" 随着大数据时代的到来,传统媒体新闻传播效能不断降低;而新兴媒体的崛起,使人们的生活和阅读方式不再单一,但也以大量的文字为主,不适合当代的碎片化阅读习惯,本项目致力于让新闻转变为图像与简短文字的结合,采用类似抖音快手瀑布流的图集滚动页面,能更加符合当代年轻人碎片化阅读的习惯。"
  91. android:textSize="18sp"
  92. android:textStyle="bold"/>
  93. <View
  94. android:layout_width="match_parent"
  95. android:layout_height="1dp"
  96. android:layout_marginTop="10dp"
  97. android:background="#DFD6D6"/>
  98. </LinearLayout>
  99. </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 提示用户相应的信息,同时将注册结果传递给调用者。

  1. package com.example.news;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.annotation.SuppressLint;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.os.Message;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11. import com.example.news.dao.UserDao;
  12. import com.example.news.entity.User;
  13. public class RegisterActivity extends AppCompatActivity {
  14. private static final String TAG = "mysql-party-register";
  15. EditText userAccount = null;
  16. EditText userPassword = null;
  17. EditText userName = null;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_register);
  22. userAccount = findViewById(R.id.userAccount);
  23. userPassword = findViewById(R.id.userPassword);
  24. userName = findViewById(R.id.userName);
  25. }
  26. public void register(View view) {
  27. String userAccount1 = userAccount.getText().toString();
  28. String userPassword1 = userPassword.getText().toString();
  29. String userName1 = userName.getText().toString();
  30. User user = new User();
  31. user.setUserAccount(userAccount1);
  32. user.setUserPassword(userPassword1);
  33. user.setUserName(userName1);
  34. new Thread() {
  35. @Override
  36. public void run() {
  37. int msg = 0;
  38. UserDao userDao = new UserDao();
  39. User uu = userDao.findUser(user.getUserAccount());
  40. if (uu != null) {
  41. msg = 1;
  42. } else {
  43. boolean flag = userDao.register(user);
  44. if (flag) {
  45. msg = 2;
  46. }
  47. }
  48. hand.sendEmptyMessage(msg);
  49. }
  50. }.start();
  51. }
  52. @SuppressLint("HandlerLeak")
  53. final Handler hand = new Handler() {
  54. public void handleMessage(Message msg) {
  55. if (msg.what == 0) {
  56. Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_LONG).show();
  57. } else if (msg.what == 1) {
  58. Toast.makeText(getApplicationContext(), "该账号已经存在,请换一个账号", Toast.LENGTH_LONG).show();
  59. } else if (msg.what == 2) {
  60. Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
  61. Intent intent = new Intent();
  62. //将想要传递的数据用putExtra封装在intent中
  63. intent.putExtra("a", "注册");
  64. setResult(RESULT_CANCELED, intent);
  65. finish();
  66. }
  67. }
  68. };
  69. }

这段代码实现了一个简单的用户登录功能的 `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` 将结果反馈给主线程处理界面更新和跳转逻辑,实现了基本的用户登录和跳转功能。

  1. package com.example.news;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.annotation.SuppressLint;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.os.Message;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12. import com.example.news.dao.UserDao;
  13. public class LoginActivity extends AppCompatActivity {
  14. private static final String TAG = "mysql-party-MainActivity";
  15. EditText EditTextAccount;
  16. EditText EditTextPassword;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_login);
  21. EditTextAccount = findViewById(R.id.uesrAccount);
  22. EditTextPassword = findViewById(R.id.userPassword);
  23. }
  24. /**
  25. *跳转到注册页面
  26. */
  27. public void reg(View view){
  28. startActivity(new Intent(getApplicationContext(), RegisterActivity.class));
  29. }
  30. /**
  31. * function: 登录
  32. * */
  33. public void login(View view){
  34. new Thread(){
  35. @Override
  36. public void run() {
  37. UserDao userDao = new UserDao();
  38. int msg = userDao.login(EditTextAccount.getText().toString(),EditTextPassword.getText().toString());
  39. hand1.sendEmptyMessage(msg);
  40. }
  41. }.start();
  42. }
  43. @SuppressLint("HandlerLeak")
  44. final Handler hand1 = new Handler() {
  45. @Override
  46. public void handleMessage(Message msg) {
  47. if (msg.what == 0){
  48. Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show();
  49. } else if (msg.what == 1) {
  50. Intent intent = new Intent(getApplicationContext(),MainActivity.class);
  51. intent.putExtra("Account",EditTextAccount.getText().toString());
  52. startActivity(intent);
  53. Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
  54. } else if (msg.what == 2){
  55. Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show();
  56. } else if (msg.what == 3){
  57. Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show();
  58. }
  59. }
  60. };
  61. }

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".menu.UpdateUserActivity">
  9. <TextView
  10. android:layout_width="match_parent"
  11. android:layout_height="50dp"
  12. android:text="编辑资料"
  13. android:gravity="center"
  14. android:textSize="20sp"
  15. android:background="#2793EA"
  16. android:textColor="#F4F2F6"/>
  17. <LinearLayout
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent"
  20. android:orientation="vertical"
  21. tools:layout_editor_absoluteX="219dp"
  22. tools:layout_editor_absoluteY="207dp"
  23. android:padding="50dp">
  24. <LinearLayout
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:orientation="horizontal">
  28. <TextView
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_weight="1"
  32. android:textSize="15sp"
  33. android:text="昵称:" />
  34. <EditText
  35. android:id="@+id/update_username"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_weight="1"
  39. android:ems="10"
  40. android:inputType="textPersonName"
  41. />
  42. </LinearLayout>
  43. <LinearLayout
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:orientation="horizontal">
  47. <TextView
  48. android:id="@+id/textView2"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:layout_weight="1"
  52. android:textSize="15sp"
  53. android:text="密码:"
  54. />
  55. <EditText
  56. android:id="@+id/update_password"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:layout_weight="1"
  60. android:ems="10"
  61. />
  62. </LinearLayout>
  63. <LinearLayout
  64. android:layout_width="match_parent"
  65. android:layout_height="wrap_content"
  66. android:orientation="horizontal">
  67. <TextView
  68. android:id="@+id/textView3"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:layout_weight="1"
  72. android:textSize="15sp"
  73. android:text="性别:"
  74. />
  75. <EditText
  76. android:id="@+id/update_sex"
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:layout_weight="1"
  80. android:ems="10"
  81. />
  82. </LinearLayout>
  83. <LinearLayout
  84. android:layout_width="match_parent"
  85. android:layout_height="wrap_content"
  86. android:orientation="horizontal">
  87. <TextView
  88. android:id="@+id/textView4"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:layout_weight="1"
  92. android:textSize="15sp"
  93. android:text="年龄:"
  94. />
  95. <EditText
  96. android:id="@+id/update_age"
  97. android:layout_width="wrap_content"
  98. android:layout_height="wrap_content"
  99. android:layout_weight="1"
  100. android:ems="10"
  101. />
  102. </LinearLayout>
  103. <LinearLayout
  104. android:layout_width="match_parent"
  105. android:layout_height="wrap_content"
  106. android:orientation="horizontal">
  107. <TextView
  108. android:id="@+id/textView5"
  109. android:layout_width="wrap_content"
  110. android:layout_height="wrap_content"
  111. android:layout_weight="1"
  112. android:textSize="15sp"
  113. android:text="邮箱:"
  114. />
  115. <EditText
  116. android:id="@+id/update_email"
  117. android:layout_width="wrap_content"
  118. android:layout_height="wrap_content"
  119. android:layout_weight="1"
  120. android:ems="10"
  121. />
  122. </LinearLayout>
  123. <Button
  124. android:layout_marginTop="50dp"
  125. android:id="@+id/btn_update"
  126. android:layout_width="match_parent"
  127. android:layout_height="wrap_content"
  128. android:text="保存"
  129. android:onClick="update"/>
  130. </LinearLayout>
  131. </LinearLayout>

五、源码获取

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/803054
推荐阅读
相关标签