赞
踩
目录
原创 新闻 项目 :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>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/803054
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。