用户还未登录或已经登录设置(保存到SharedPreferences)

登录成功后就会调回一个已经登录的页面把数据保存在SharedPreferences从服务器获取图头像和名字放在对应的地方

还未登录会弹出一个对话框如下

点击确定会跳入

代码如下

package p2ppinves.com.p2p.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import butterknife.Bind;
import butterknife.ButterKnife;
import p2ppinves.com.p2p.R;
import p2ppinves.com.p2p.activity.LoginActivity;
import p2ppinves.com.p2p.bean.User;
import p2ppinves.com.p2p.common.BaseActivity;
import p2ppinves.com.p2p.common.BaseFragment;
import p2ppinves.com.p2p.util.UIUtils;

/**
 * Created by 联想 on 2017/5/12.
 */

public class MeFragment extends BaseFragment {
    @Bind(R.id.iv_title_back)
    ImageView ivTitleBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_setting)
    ImageView ivTitleSetting;
    @Bind(R.id.iv_me_icon)
    ImageView ivMeIcon;
    @Bind(R.id.rl_me_icon_time)
    RelativeLayout rlMeIconTime;
    @Bind(R.id.tv_me_name)
    TextView tvMeName;
    @Bind(R.id.rl_me)
    RelativeLayout rlMe;
    @Bind(R.id.recharge)
    ImageView recharge;
    @Bind(R.id.withdraw)
    ImageView withdraw;
    @Bind(R.id.ll_touzi)
    TextView llTouzi;
    @Bind(R.id.ll_touzi_zhiguan)
    TextView llTouziZhiguan;
    @Bind(R.id.ll_zichang)
    TextView llZichang;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return null;
    }

    @Override
    protected void initdata(String content) {
//判断用户是否已经登录
        isLogin();
    }
    public void isLogin() {
//查看本地是否有用户的登录信息
        SharedPreferences sp = this.getActivity().getSharedPreferences("user_info", Context.MODE_PRIVATE);
        String name = sp.getString("name", "");

        if(TextUtils.isEmpty(name)){
            //表示用户没有登录过
            //本地没有保存过用户信息
            doLogin();
        }else {
            //已经登陆过,则直接加载用户的信息并保存
            doUser();
        }
    }

    private void doUser() {
//读取本地的保存信息
        User user = ((BaseActivity) this.getActivity()).readUser();
        //2获取对象的信息,并设置
        tvMeName.setText(user.getName());
        Picasso.with(this.getActivity()).load(user.getImageUrl()).into(ivMeIcon);
        Log.e("TAA",user.getImageUrl());

    }

    //提示用户登录
    private void doLogin() {
        new AlertDialog.Builder(this.getActivity())
                .setTitle("提示")
                .setMessage("您好,还没有登录哦!")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    //    UIUtils.toast("进入登录页面");
                        ((BaseActivity)MeFragment.this.getActivity()).gotoActivity(LoginActivity.class,null);
                    }
                }).setCancelable(false)//让用户不可以取消
                .show();
    }

    public void inittitle() {
        ivTitleBack.setVisibility(View.GONE);
        tvTitle.setText("我的资产");
        ivTitleSetting.setVisibility(View.GONE);
    }



}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/common_title" />

    <p2ppinves.com.p2p.ui.MyScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <RelativeLayout
                android:id="@+id/rl_me"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@color/my_title_bg"
                android:gravity="center">

                <RelativeLayout
                    android:id="@+id/rl_me_icon_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true">

                    <ImageView
                        android:layout_width="64dp"
                        android:layout_height="64dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/my_user_bg_icon" />

                    <ImageView
                        android:id="@+id/iv_me_icon"
                        android:layout_width="62dp"
                        android:layout_height="62dp"
                        android:layout_centerInParent="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/my_user_default" />
                </RelativeLayout>

                <TextView
                    android:id="@+id/tv_me_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/rl_me_icon_time"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:text="Hi,wecome!"
                    android:textColor="@color/white"
                    android:textSize="14sp" />
            </RelativeLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="学习提示:"
                    android:textColor="@android:color/holo_red_dark" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="充值我们将以集成支付宝sdk支付金额演示充值功能" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:text="提现业务:在p2p金融平台中,提现都不是实时到账的,而是审核提现,系统将会告知24小时内进行转账,如果未收到,请联系客服..." />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:gravity="center">

                <ImageView
                    android:id="@+id/recharge"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="9dp"
                    android:layout_weight="1"
                    android:src="@drawable/my_recharge_bg" />

                <ImageView
                    android:id="@+id/withdraw"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="9dp"
                    android:layout_weight="1"
                    android:src="@drawable/my_withdraw_bg" />
            </LinearLayout>

            <!--充分使用textview属性代替简单LinearLayout布局-->
            <TextView
                android:id="@+id/ll_touzi"
                style="@style/LoginStyle"
                android:drawableLeft="@drawable/my_invest_manage"
                android:text="投资管理" />

            <TextView
                android:id="@+id/ll_touzi_zhiguan"
                style="@style/LoginStyle"
                android:drawableLeft="@drawable/my_invest_manage"
                android:text="投资管理(直观)" />

            <TextView
                android:id="@+id/ll_zichang"
                style="@style/LoginStyle"
                android:drawableLeft="@drawable/my_asset_manage"
                android:text="资产管理" />

        </LinearLayout>
    </p2ppinves.com.p2p.ui.MyScrollView>

</LinearLayout>

User类

public class User {
    public int id;//编号
    public String name;//姓名
    public String password;//密码
    public String phone;//手机号
    public String imageUrl;//头像地址
    public boolean isCredit;//是否公安部认证

    public User() {
    }

    public User(int id, String name, String password, String phone, String imageUrl, boolean isCredit) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.phone = phone;
        this.imageUrl = imageUrl;
        this.isCredit = isCredit;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public boolean isCredit() {
        return isCredit;
    }

    public void setCredit(boolean credit) {
        isCredit = credit;
    }
}

写一个BaseActivity继承FragmentActivity(里面的用来简化代码和存放需要调用的代码)

package p2ppinves.com.p2p.common;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.loopj.android.http.AsyncHttpClient;

import butterknife.ButterKnife;
import p2ppinves.com.p2p.R;
import p2ppinves.com.p2p.bean.User;
import p2ppinves.com.p2p.util.UIUtils;

/**
 * Created by 联想 on 2017/5/20.
 */

public abstract class BaseActivity extends FragmentActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        //将当前的activity添加到ActivityManager中
        ActivityManager.getInstance().addActivity(this);

        initTitle();

        initDate();

    }
public AsyncHttpClient client=new AsyncHttpClient();
    protected abstract void initDate();

    protected abstract void initTitle();

    public abstract int getLayoutId() ;


    //启动新的Activity
    public  void gotoActivity(Class Activity, Bundle bundle){
        Intent intent=new Intent(this,Activity);

        //携带数据
        if(bundle !=null && bundle.size()!=0){
            intent.putExtra("data",bundle);
        }
        startActivity(intent);
    }
    //销毁当前的Activity
    public void removeCurrentActivity(Activity activity){
ActivityManager.getInstance().removeCurrent();

    }

    //销毁所有的activity
    public  void removeAll(){
        ActivityManager.getInstance().removeAll();
    }


    //保存用户信息的操作:使用sp存储
    public void saveUser(User user){
        SharedPreferences sp = this.getSharedPreferences("user_info", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("name",user.name);
        editor.putString("imageurl",AppNetConfig.BASE_URL+user.imageUrl);
        editor.putBoolean("iscredit", user.isCredit);
        editor.putString("phone",user.phone);
        editor.commit();//只有提交以后,才可以创建此文件,并保存数据
    }

    //读取数据,得到内存中的User对象
    public User readUser(){
        User user = new User();
        SharedPreferences sp = this.getSharedPreferences("user_info", Context.MODE_PRIVATE);
        user.name = sp.getString("name","");
        user.imageUrl = sp.getString("imageurl","");
        user.isCredit = sp.getBoolean("iscredit",false);
        user.phone = sp.getString("phone","");

        return user;
    }

}

转载于:https://my.oschina.net/u/3486497/blog/905300

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值