Android开发日记(三) 个人中心

一、用户数据的存储

使用SharedPreference来保存用户登录状态和信息是个不错的选择, 它采用键值对的方式存储数据, 负责登录模块的同学会在用户登陆之后向名为 info 的SharedPreference中填入用户的手机号信息phone, 然后在主活动中请求服务器得到当前登录用户除密码外的所有信息, 由于目前服务器端还没有做好, 先自行写入康师傅的数据

private SharedPreferences spf;

private void getUserInfo() {
    
    
      Request request = new Request.Builder().url("http://www.baidu.com").build();
      client.newCall(request).enqueue(new Callback() {
    
    
          @Override
          public void onFailure(Request request, IOException e) {
    
    
              runOnUiThread(new Runnable() {
    
    
                  @Override
                  public void run() {
    
    
                      Toast.makeText(MainActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                  }
              });
          }

          @Override
          public void onResponse(Response response) throws IOException {
    
    
              final String name = "康纳";
              String phone = "12345678910";
              String email = "";

              SharedPreferences.Editor editor = spf.edit();
              editor.putString("phone", phone);
              editor.putString("name", name);
              editor.putString("email", email);
              editor.apply();

              runOnUiThread(new Runnable() {
    
    
                  @Override
                  public void run() {
    
    
                      navUserName.setText(name);    // 更新侧边栏头部用户名
                  }
              });
          }
      });
  }

二、个人中心布局

个人中心提供用户信息的查看和修改, 每一项都有很高的相似性, 类似于这样在这里插入图片描述
这样的布局如果一项一项写可就太麻烦了, 可以用自定义控件的方式定制一个通用的格式, 然后在布局中引入就可以了

  1. 在layout文件夹新建item_personal_menu.xml作为布局格式:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:layout_marginStart="20dp"
        android:src="@drawable/setting"
        />

    <TextView
        android:id="@+id/name"
        android:textColor="#000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/icon"
        android:layout_marginStart="10dp"/>

    <ImageView
        android:id="@+id/more"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:src="@drawable/more"
        android:layout_marginEnd="10dp"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@id/more"
        android:layout_marginEnd="10dp"
        />

    <ImageView
        android:id="@+id/line"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#c2c2c2"
        android:layout_alignParentBottom="true"
        android:visibility="gone"/>

</RelativeLayout>
  1. 在values文件夹下的attrs文件(如果没有的话可以新建一个)中新建一项declare-styleable用来控制自定义控件的属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="PersonaltemView">
        <attr name="icon" format="reference"/>//设置左侧图标
        <attr name="name" format="string"/>//左侧标题文字
        <attr name="data" format="string"/>//右侧描述文字
        <attr name="show_more" format="boolean"/>//是否显示右侧小箭头
        <attr name="show_line" format="boolean"/>//是否显示下划线
    </declare-styleable>
</resources>
  1. 新建类PersonalItemView继承上面布局文件的最外层布局, 在其中实现对自定义控件属性的设置:
/**
 * 自定义个人中心选项控件
 */
public class PersonalItemView extends RelativeLayout {
    
    
    private TextView data;

    public PersonalItemView(final Context context, AttributeSet attrs){
    
    
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.item_personal_menu, this);
        @SuppressLint("CustomViewStyleable") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PersonaltemView);

        ImageView icon = findViewById(R.id.icon);
        ImageView more = findViewById(R.id.more);
        ImageView line = findViewById(R.id.line);
        TextView name = findViewById(R.id.name);
        data = findViewById(R.id.data);

        icon.setImageDrawable(typedArray.getDrawable(R.styleable.PersonaltemView_icon));
        name.setText(typedArray.getText(R.styleable.PersonaltemView_name));

        if (typedArray.getBoolean(R.styleable.PersonaltemView_show_more, false)){
    
    
            more.setVisibility(VISIBLE);
        }
        if (typedArray.getBoolean(R.styleable.PersonaltemView_show_line, false)){
    
    
            line.setVisibility(VISIBLE);
        }
        typedArray.recycle();
    }

    // 提供设置控件的描述数据
    public void setData(String data){
    
    
        this.data.setText(data);
    }
}
  1. 在主布局文件中引入自定义控件, 就像这样:
<com.example.shenshen1.PersonalItemView
        android:id="@+id/item_personal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:icon="@drawable/people"
        app:name="用户名"
        android:background="#fff"
        app:show_more="true"
        app:show_line="true"/>

这里需要注意由于使用了自定义控件, 需要指定新的命名空间: xmlns:app="http://schemas.android.com/apk/res-auto"

  1. 自定义控件中定义了setData方法用来设置右侧的描述文字, 在Activity中从SharedPreference中取出数据使用这个方法设置数据
    另外以后修改个人信息实现后希望回到这个活动的时候可以立即更新数据, 所以这部分代码应该放在 onResume 方法内
@Override
protected void onResume() {
    
    
    super.onResume();
    spf = getSharedPreferences("info", MODE_PRIVATE);
    String phone = spf.getString("phone", "");
    String name = spf.getString("name", "");
    String email = spf.getString("email", "");

    userName.setData(name);
    userPhone.setData(phone);
    userEmail.setData(email);
}

三、效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zzh2910/article/details/88542786
今日推荐