今天完成了“我的”界面的设计与功能实现,主要可以修改个人信息、查看系统功能等
WodeFragment.java
package com.example.share;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class WodeFragment extends Fragment {

private TextView textUsername;
private Button btnEditProfile, btnChangePassword, btnSettings, btnAboutUs;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_wode, container, false);

    // 初始化控件
    textUsername = view.findViewById(R.id.text_username);
    btnEditProfile = view.findViewById(R.id.btn_edit_profile);
    btnChangePassword = view.findViewById(R.id.btn_change_password);
    btnSettings = view.findViewById(R.id.btn_settings);
    btnAboutUs = view.findViewById(R.id.btn_about_us);

    // 设置按钮点击事件
    btnEditProfile.setOnClickListener(buttonClickListener);
    btnChangePassword.setOnClickListener(buttonClickListener);
    btnSettings.setOnClickListener(buttonClickListener);
    btnAboutUs.setOnClickListener(buttonClickListener);

    // 假设用户登录后从本地获取用户名并显示
    String username = getUsernameFromLocal();
    textUsername.setText(username);

    return view;
}

private View.OnClickListener buttonClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // 根据点击的按钮执行相应操作
        int id = v.getId();
        if (id == R.id.btn_edit_profile) {
            // 编辑资料操作
        } else if (id == R.id.btn_change_password) {
            // 修改密码操作
        } else if (id == R.id.btn_settings) {
            // 设置操作
        } else if (id == R.id.btn_about_us) {
            // 关于我们操作
        }
    }
};

// 从本地获取用户名,这里只是示例,实际应用中需要替换为真正的逻辑
private String getUsernameFromLocal() {
    // 假设从SharedPreferences中获取用户名
    SharedPreferences sharedPref = getActivity().getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
    String username = "欢迎您,"+sharedPref.getString("username", "")+"!"; // 获取存储的用户名,如果没有则默认为空字符串
    // 你需要根据实际情况实现这个方法
    return username;
}

}
fragment_wode.xml

<!-- 用户名 -->
<TextView
    android:id="@+id/text_username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="用户名"
    android:textSize="20sp"
    android:layout_marginTop="20dp"
    android:layout_centerHorizontal="true"/>

<!-- 编辑资料按钮 -->
<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/btn_edit_profile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="编辑资料"
    android:layout_below="@id/text_username"
    android:layout_marginTop="20dp"/>

<!-- 修改密码按钮 -->
<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/btn_change_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="修改密码"
    android:layout_below="@id/btn_edit_profile"
    android:layout_marginTop="10dp"/>

<!-- 设置按钮 -->
<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/btn_settings"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="设置"
    android:layout_below="@id/btn_change_password"
    android:layout_marginTop="10dp"/>

<!-- 关于我们按钮 -->
<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/btn_about_us"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="关于我们"
    android:layout_below="@id/btn_settings"
    android:layout_marginTop="10dp"/>
posted on 2024-06-28 22:07  一点都不难  阅读(24)  评论(0)    收藏  举报