MyFragmeny.java文件中这样写
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
root = inflater.inflate(R.layout.fragment_my, container, false);
initView();
user = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
if (login==0)Login(); {
myLogin.setOnClickListener(v -> {
if (login==0) {
Login();
}else if(login==1){
login = 0;
SharedPreferences.Editor editor=user.edit();
editor.putString("username","");
editor.putString("password","");
editor.putString("token","");
editor.putString("userId","");
editor.apply();
startActivity(new Intent(getContext(),My_LoginActivity.class));
}
});
myGerenxinxi.setOnClickListener(v -> {
if (login==0){
Login();
}else {
startActivity(new Intent(getContext(),My_LoginActivity.class));
}
});
myDingdanliebiao.setOnClickListener(v -> {
if (login==0){
Login();
}else {
startActivity(new Intent(getContext(),My_LoginActivity.class));
}
});
myXiugaimima.setOnClickListener(v -> {
if (login==0){
Login();
}
else {
startActivity(new Intent(getContext(),My_LoginActivity.class));
}
});
myTijiaofankui.setOnClickListener(v -> {
if (login==0){
Login();
}else {
startActivity(new Intent(getContext(),My_LoginActivity.class));
}
});
}
return root;
}
@Override
public void onResume() {
super.onResume();
if (login==0){
myLogin.setText("登录");
}else {
myLogin.setText("退出账号");
getMyUserInfoData();
}
}
// 用户信息
private void getMyUserInfoData() {
RetrofitClient.apiService().getUserInfoData(user.getString("token","")).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
MyUserInfoBean body = response.body();
if (body!=null&&body.getCode()==200) {
MyUserInfoBean.UserBean user = body.getUser();
// 回传一个用户名
myWeidenglu.setText(user.getNickName());
// if (!user.getAvatar().equals("")){
// Glide.with(getActivity()).load(RetrofitClient.IP+user.getAvatar())
// .centerCrop()
// .into()
// }
}
}
@Override
public void onFailure(Call call, Throwable throwable) {
}
});
}
// 登录状态
private void Login() {
Toast.makeText(getContext(),"尚未登录",Toast.LENGTH_SHORT).show();
startActivity(new Intent(getContext(), My_LoginActivity.class));
}
private void initView() {
myTouxiang = (ImageView) root.findViewById(R.id.my_touxiang);
myWeidenglu = (TextView) root.findViewById(R.id.my_weidenglu);
myGerenxinxi = (LinearLayout) root.findViewById(R.id.my_gerenxinxi);
myDingdanliebiao = (LinearLayout)root. findViewById(R.id.my_dingdanliebiao);
myXiugaimima = (LinearLayout) root.findViewById(R.id.my_xiugaimima);
myTijiaofankui = (LinearLayout) root.findViewById(R.id.my_tijiaofankui);
myLogin = (Button) root.findViewById(R.id.my_login);
}
}



