Android Development Diary (3) Personal Center

1. Storage of user data

It is a good choice to use SharedPreference to save the user's login status and information. It uses key-value pairs to store data. The students in charge of the login module will fill in the user's mobile phone number in the SharedPreference named info after the user logs in, phone, Then in the main activity, request the server to get all the information of the currently logged-in user except the password. Since the server has not been done yet, first write the data of Master Kong by yourself

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);    // 更新侧边栏头部用户名
                  }
              });
          }
      });
  }

2. Personal center layout

The personal center provides user information viewing and modification. Each item has a high similarity. Insert picture description here
A layout similar to this is too troublesome to write one by one. You can customize a universal one by customizing the control. Format, and then import it in the layout

  1. Create item_personal_menu.xml in the layout folder as the layout format:
<?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. Create a new declare-styleable in the attrs file under the values ​​folder (you can create a new one if you don't have one) to control the attributes of the custom control:
<?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. The new class PersonalItemView inherits the outermost layout of the above layout file, and implements the setting of custom control properties in it:
/**
 * 自定义个人中心选项控件
 */
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. Introduce custom controls in the main layout file, like this:
<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"/>

It should be noted here that due to the use of custom controls, a new namespace needs to be specified: xmlns:app="http://schemas.android.com/apk/res-auto"

  1. The setData method is defined in the custom control to set the description text on the right side. Use this method to set the data by fetching the data from the SharedPreference in the Activity.
    In addition, after the personal information is modified in the future, the data can be updated immediately when you want to return to this activity, so This part of the code should be placed in the onResume method
@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);
}

Three, the effect

Insert picture description here

Guess you like

Origin blog.csdn.net/zzh2910/article/details/88542786