1. 代码具体实现
- 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:orientation="vertical"
tools:context=".fragment.HomeFragment">
<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.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="130dp"
android:background="@color/purple_200"
android:backgroundTint="@color/purple_200"
app:cardCornerRadius="15dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/ll_mine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<androidx.cardview.widget.CardView
android:layout_width="78dp"
android:layout_height="78dp"
android:layout_marginLeft="20dp"
app:cardCornerRadius="39dp"
app:cardElevation="0dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/img_logo" />
</androidx.cardview.widget.CardView>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="浩宇软件开发"
android:textColor="@color/white"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_nickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="这个家伙很懒,什么都没有留下~~"
android:textColor="@color/white"
android:textSize="12sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginTop="10dp"
android:background="#f5f5f5" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="10dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="优惠券"
android:textColor="#444444"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="购票享优惠"
android:textSize="12sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="影城卡"
android:textColor="#444444"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="开卡立省"
android:textSize="12sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="猫享卡"
android:textColor="#444444"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="去绑卡"
android:textSize="12sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="猫币"
android:textColor="#444444"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="购票送喵币"
android:textSize="12sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
<View
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginTop="10dp"
android:background="#f5f5f5" />
<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="修改密码" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f5f5f5" />
<TextView
android:id="@+id/btn_order"
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="我的订单" />
<View
android:layout_width="match_parent"
android:layout_height="10dp"
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="清理缓存" />
<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" />
<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>
- MineFragment.java页面
public class MineFragment extends Fragment {
private View rootView;
private TextView tv_username;
private TextView tv_nickname;
private LinearLayoutCompat ll_mine;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_mine, container, false);
tv_username = rootView.findViewById(R.id.tv_username);
tv_nickname = rootView.findViewById(R.id.tv_nickname);
ll_mine = rootView.findViewById(R.id.ll_mine);
ll_mine.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UserInfo userInfo = UserInfo.getUserInfo();
if (null == userInfo) {
startActivity(new Intent(getActivity(), LoginActivity.class));
}
}
});
rootView.findViewById(R.id.btn_order).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UserInfo userInfo = UserInfo.getUserInfo();
if (null != userInfo) {
startActivity(new Intent(getActivity(), OrderActivity.class));
} else {
Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getActivity(), LoginActivity.class));
}
}
});
rootView.findViewById(R.id.btn_edit_pwd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UserInfo userInfo = UserInfo.getUserInfo();
if (null != userInfo) {
Intent intent = new Intent(getActivity(), UpdatePwdActivity.class);
startActivityForResult(intent, 2000);
} else {
startActivity(new Intent(getActivity(), LoginActivity.class));
Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
}
}
});
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) {
Toast.makeText(getActivity(), "清理成功", Toast.LENGTH_SHORT).show();
}
});
rootView.findViewById(R.id.btn_exit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getContext()).setTitle("温馨提示").setMessage("确定要退出登录吗?").setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
Toast.makeText(getActivity(), "退出登录成功~~", Toast.LENGTH_SHORT).show();
UserInfo.setUserInfo(null);
}
})
.show();
}
});
return rootView;
}
@Override
public void onResume() {
super.onResume();
UserInfo userInfo = UserInfo.getUserInfo();
if (null != userInfo) {
tv_nickname.setVisibility(View.VISIBLE);
tv_username.setText(userInfo.getUsername());
tv_nickname.setText(userInfo.getNickname());
} else {
tv_username.setText("请先登录");
tv_nickname.setVisibility(View.GONE);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 2000) {
Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
}
}
}
2. 运行效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/c5fa529c651449bf894b54a63d001297.png)