Android开发,待办事项提醒App的设计与实现(个人中心页)
文章目录
- 1. 编写UI布局
- 2. 实现逻辑
- 3. 运行效果图
- 3. 关于作者其它项目视频教程介绍
- Android开发,待办事项提醒App的设计与实现: https://blog.csdn.net/jky_yihuangxing/article/details/145277956?spm=1001.2014.3001.5501
1. 编写UI布局
- fragment_mine.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:background="#fefefe"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
app:title="个人中心"
app:titleTextColor="@color/white" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="140dp"
app:cardElevation="0dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient_background5"
android:gravity="center_vertical">
<androidx.cardview.widget.CardView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="16dp"
app:cardCornerRadius="40dp"
app:cardElevation="0dp">
<ImageView
android:id="@+id/avatar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/ic_avatar" />
</androidx.cardview.widget.CardView>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录/注册"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这个家伙很懒,什么都没有留下~"
android:textColor="@color/white"
android:textSize="13sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
<TextView
android:id="@+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:drawableLeft="@drawable/ic_baseline_edit_24"
android:gravity="center_vertical"
android:textColor="@color/white"
android:padding="4dp"
android:text=" 编辑" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/btn_edit_pwd"
android:layout_width="match_parent"
android:layout_height="46dp"
android:drawableRight="@drawable/ic_baseline_keyboard_arrow_right_24"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="修改密码"
android:textColor="#444444" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f5f5f5" />
<TextView
android:id="@+id/btn_clear"
android:layout_width="match_parent"
android:layout_height="46dp"
android:drawableRight="@drawable/ic_baseline_keyboard_arrow_right_24"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="清理缓存"
android:textColor="#444444" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#f5f5f5"
app:cardCornerRadius="10dp"
app:cardElevation="0dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@color/white"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="常用服务"
android:textColor="#333333" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="28dp"
android:layout_height="28dp"
android:src="@drawable/ic_baseline_headphones_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="官方客服"
android:textColor="#666666"
android:textSize="13sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="#f5f5f5" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="28dp"
android:layout_height="28dp"
android:src="@drawable/ic_baseline_shopping_bag_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="积分兑换"
android:textColor="#666666"
android:textSize="13sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="#f5f5f5" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="28dp"
android:layout_height="28dp"
android:src="@drawable/ic_baseline_help_outline_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="帮助中心"
android:textColor="#666666"
android:textSize="13sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<View
android:layout_width="1dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:background="#f5f5f5" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="28dp"
android:layout_height="28dp"
android:src="@drawable/ic_baseline_settings_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="设置"
android:textColor="#666666"
android:textSize="13sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView>
<TextView
android:layout_width="match_parent"
android:layout_height="46dp"
android:drawableRight="@drawable/ic_baseline_keyboard_arrow_right_24"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="意见反馈"
android:textColor="#444444" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f5f5f5" />
<TextView
android:layout_width="match_parent"
android:layout_height="46dp"
android:drawableRight="@drawable/ic_baseline_keyboard_arrow_right_24"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="系统设置"
android:textColor="#444444" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f5f5f5" />
<TextView
android:id="@+id/btn_about"
android:layout_width="match_parent"
android:layout_height="46dp"
android:drawableRight="@drawable/ic_baseline_keyboard_arrow_right_24"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="关于APP"
android:textColor="#444444" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f5f5f5" />
<TextView
android:id="@+id/btn_exit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:background="#f5f5f5"
android:gravity="center"
android:text="退出登录"
android:textStyle="bold" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.core.widget.NestedScrollView>
</androidx.appcompat.widget.LinearLayoutCompat>
2. 实现逻辑
MineFragment.java
- 加载布局
@Override
protected int getLayoutResId() {
return R.layout.fragment_mine;
}
- 初始化控件
@Override
protected void initView() {
//初始化控件
tv_username = rootView.findViewById(R.id.tv_username);
tv_nickname = rootView.findViewById(R.id.tv_nickname);
}
- 事件监听
@Override
protected void setListener() {
//关于app
rootView.findViewById(R.id.btn_about).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getActivity(), AboutActivity.class));
}
});
//清理缓存
rootView.findViewById(R.id.btn_clear).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToast("清理成功~");
}
});
//修改密码
rootView.findViewById(R.id.btn_edit_pwd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), UpdatePwdActivity.class);
startActivityForResult(intent, 2000);
}
});
//编辑用户信息
rootView.findViewById(R.id.btn_edit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), EditUserInfoActivity.class);
intent.putExtra("userInfo", UserInfo.getUserInfo());
startActivityForResult(intent, 3000);
}
});
//退出登录
rootView.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (UserInfo.getUserInfo() != null) {
new AlertDialog.Builder(getActivity()).setTitle("操作").setMessage("确定要退出登录吗?").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (null != getActivity()) {
UserInfo.setUserInfo(null);
startActivity(new Intent(getActivity(), LoginActivity.class));
showToast("退出登录成功~");
}
}
}).show();
} else {
showToast("请先登录");
}
}
});
}
- 初始化数据,获取登录用户信息
@Override
protected void initData() {
//设置用户信息
setUserInfo();
}
/**
* 设置用户信息
*/
private void setUserInfo() {
UserInfo userInfo = UserInfo.getUserInfo();
if (null != userInfo) {
tv_username.setText(userInfo.getUsername());
tv_nickname.setText(userInfo.getNickname());
}
}
- 页面回传刷新用户信息
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 2000 && null != getActivity()) {
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
getActivity().finish();
} else if (resultCode == 3000 && null != getActivity()) {
//重新获取用户数据
UserInfo.setUserInfo(UserDbHelper.getInstance(getActivity()).login(UserInfo.getUserInfo().getUsername()));
setUserInfo();
}
}
温馨提示:代码中涉及了BaseFragment的封装和用户登录注册UserDbHelper数据库的实现,这里不展开详情说,可以关注作者视频教程,最后给到教程视频地址
3. 运行效果图
3. 关于作者其它项目视频教程介绍
- Android新闻资讯app实战:https://www.bilibili.com/video/BV1CA1vYoEad/?vd_source=984bb03f768809c7d33f20179343d8c8
- Androidstudio开发购物商城实战:https://www.bilibili.com/video/BV1PjHfeXE8U/?vd_source=984bb03f768809c7d33f20179343d8c8
- Android开发备忘录记事本实战:https://www.bilibili.com/video/BV1FJ4m1u76G?vd_source=984bb03f768809c7d33f20179343d8c8&spm_id_from=333.788.videopod.sections
- Androidstudio底部导航栏实现:https://www.bilibili.com/video/BV1XB4y1d7et/?spm_id_from=333.337.search-card.all.click&vd_source=984bb03f768809c7d33f20179343d8c8
- Android使用TabLayout+ViewPager2实现左右滑动切换:https://www.bilibili.com/video/BV1Mz4y1c7eX/?spm_id_from=333.337.search-card.all.click&vd_source=984bb03f768809c7d33f20179343d8c8
- 为什么要封装BaseActivity?: https://www.bilibili.com/video/BV11S411A7R5/?vd_source=984bb03f768809c7d33f20179343d8c8
- 为什么要封装BaseFragment?:https://www.bilibili.com/video/BV1Um421G7yC/?vd_source=984bb03f768809c7d33f20179343d8c8