23.实战演练--个人主页

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LoginTest"
        tools:targetApi="31">
        <activity
            android:name=".EditProfileActivity"
            android:exported="false" />
        <activity
            android:name=".UserProfileActivity"
            android:exported="false" />
        <activity
            android:name=".LoginActivity"
            android:exported="true"
            android:label="登录">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".RegisterActivity"
            android:exported="false"
            android:label="注册" />
        <activity
            android:name=".MainActivity"
            android:exported="false"
            android:label="首页" />
    </application>

</manifest>
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyBtnStyle">
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">25sp</item>
        <item name="android:background">@drawable/btn_bg_selector</item>
        <item name="android:layout_marginTop">20dp</item>
        <item name="android:layout_marginRight">20dp</item>
        <item name="android:layout_marginLeft">20dp</item>
    </style>

    <style name="MyEditStyle">
        <item name="android:textSize">18sp</item>
        <item name="android:background">@drawable/edit_text_bg</item>
        <item name="android:paddingLeft">10dp</item>
        <item name="android:layout_height">50dp</item>
    </style>
</resources>
XML 复制代码
<resources>
    <string name="app_name">LoginTest</string>
    
    <string-array name="cities">
        <item>北京</item>
        <item>上海</item>
        <item>天津</item>
        <item>深圳</item>
        <item>广州</item>
        <item>福建</item>
        <item>江苏</item>
        <item>浙江</item>
        <item>江西</item>
        <item>湖北</item>
    </string-array>
</resources>
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>

    <color name="green_200">#C5E1A5</color>
    <color name="green_500">#8BC34A</color>
    <color name="green_700">#689F38</color>

    <color name="colorPrimary">@color/green_500</color>
    <color name="colorPrimaryDark">@color/green_700</color>
    <color name="accent">#F4511E</color>

    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
</resources>
XML 复制代码
<vector android:autoMirrored="true" android:height="24dp"
    android:tint="#093243" android:viewportHeight="24"
    android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M6.23,20.23l1.77,1.77l10,-10l-10,-10l-1.77,1.77l8.23,8.23z"/>
</vector>
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/colorPrimary"/>
    <item android:state_pressed="false" android:drawable="@color/colorPrimaryDark"/>
</selector>
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="3dp" android:color="@color/colorPrimary"/>

    <corners android:radius="10dp"/>
</shape>
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:layout_marginTop="30dp"
        android:text="欢迎你:"
        android:layout_gravity="center_horizontal"/>
    <Button
        android:id="@+id/btn_logout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="退出登录"
        android:textSize="25sp"
        android:layout_margin="20dp"
        android:background="@drawable/btn_bg_selector"/>
</LinearLayout>
java 复制代码
package com.example.logintest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_logout;
    private TextView tvContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_logout = findViewById(R.id.btn_logout);
        btn_logout.setOnClickListener(this);

        tvContent = findViewById(R.id.tv_content);

        Intent intent = getIntent();
        String account = intent.getStringExtra("account");
        tvContent.setText("欢迎你:"+account);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_logout) {
            SharedPreferences spf = getSharedPreferences("spfRecord",MODE_PRIVATE);
            SharedPreferences.Editor edit = spf.edit();
            edit.putBoolean("isLogin",false);
            edit.apply();

            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            this.finish();
        }
    }
}
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RegisterActivity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账        号:"
            android:textSize="25sp"/>
        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="请输入用户名或手机号"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:paddingLeft="5dp"
            android:inputType="text"
            android:background="@drawable/edit_text_bg"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密        码:"
            android:textSize="25sp"/>
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="请输入密码"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:paddingLeft="5dp"
            android:inputType="numberPassword"
            android:background="@drawable/edit_text_bg"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认密码:"
            android:textSize="25sp"/>
        <EditText
            android:id="@+id/et_password_confirm"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="再次输入密码"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:paddingLeft="5dp"
            android:inputType="numberPassword"
            android:background="@drawable/edit_text_bg"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn_register"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:textSize="25sp"
        android:background="@drawable/btn_bg_selector"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>
    <CheckBox
        android:id="@+id/rb_agree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:text="同意用户协议?"
        android:layout_gravity="left"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"/>
</LinearLayout>
java 复制代码
package com.example.logintest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnRegister;
    private EditText etAccount,etPass,etPassConfirm;
    private CheckBox rbAgree;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        btnRegister = findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(this);

        etAccount = findViewById(R.id.et_account);
        etAccount.setOnClickListener(this);

        etPass = findViewById(R.id.et_password);
        etPass.setOnClickListener(this);

        etPassConfirm = findViewById(R.id.et_password_confirm);
        etPassConfirm.setOnClickListener(this);

        rbAgree = findViewById(R.id.rb_agree);
        rbAgree.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        String name = etAccount.getText().toString();
        String pass = etPass.getText().toString();
        String passConfirm = etPassConfirm.getText().toString();
        if (view.getId() == R.id.btn_register) {
            if (TextUtils.isEmpty(name)) {
                Toast.makeText(RegisterActivity.this, "用户名不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            if (TextUtils.isEmpty(pass)) {
                Toast.makeText(RegisterActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
                return;
            }
            if (!TextUtils.equals(pass, passConfirm)) {
                Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_LONG).show();
                return;
            }
            if (!rbAgree.isChecked()) {
                Toast.makeText(RegisterActivity.this, "请同意用户协议", Toast.LENGTH_LONG).show();
                return;
            }

            SharedPreferences spf = getSharedPreferences("spfRecorid",MODE_PRIVATE);
            SharedPreferences.Editor edit = spf.edit();
            edit.putString("account",name);
            edit.putString("password",pass);

            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString("account",name);
            bundle.putString("password",pass);
            intent.putExtras(bundle);
            setResult(0,intent);

            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
            this.finish();
        }
    }
}
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:textSize="25sp"/>
        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="请输入用户名或手机号"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:paddingLeft="5dp"
            android:inputType="text"
            android:background="@drawable/edit_text_bg"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp"
        android:gravity="center_vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="25sp"/>
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="请输入密码"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:paddingLeft="5dp"
            android:inputType="numberPassword"
            android:background="@drawable/edit_text_bg"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">
        <CheckBox
            android:id="@+id/cb_remember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"/>
        <CheckBox
            android:id="@+id/cb_auto_login"
            android:visibility="visible"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自动登录"
            android:layout_marginLeft="40dp"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn_Login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        style="@style/MyBtnStyle"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"/>
    <TextView
        android:id="@+id/to_register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"
        android:text="还没有账号?"
        android:layout_gravity="right"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"/>

</LinearLayout>
java 复制代码
package com.example.logintest;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    public static final int REQUEST_CODE_REGISTER = 1;
    private Button btnLogin;
    private EditText etAccount,etPassword;
    private CheckBox cbRemember,cbAutoLogin;
    private TextView toRegister;

    private String userName = "admin";
    private String pass = "1234";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_Login) {
            String account = etAccount.getText().toString();
            String passWord = etPassword.getText().toString();
            if (TextUtils.isEmpty(userName)){
                Toast.makeText(LoginActivity.this, "对不起,你还没注册账号!", Toast.LENGTH_LONG).show();
                return;
            }
            if (TextUtils.equals(account, userName)) {
                if (TextUtils.equals(passWord, pass)) {
                    Toast.makeText(LoginActivity.this, "恭喜你,登陆成功!", Toast.LENGTH_LONG).show();
                    if (cbRemember.isChecked()){
                        SharedPreferences spf = getSharedPreferences("spfRecord",MODE_PRIVATE);
                        SharedPreferences.Editor edit = spf.edit();
                        edit.putString("account",account);
                        edit.putString("password",passWord);
                        edit.putBoolean("isRemember",true);
                        if (cbAutoLogin.isChecked()){
                            edit.putBoolean("isLogin",true);
                        }else {
                            edit.putBoolean("isLogin",false);
                        }
                        edit.apply();
                    }else {
                        SharedPreferences spf = getSharedPreferences("spfRecord",MODE_PRIVATE);
                        SharedPreferences.Editor edit = spf.edit();
                        edit.putBoolean("isRemember",false);
                        edit.apply();
                    }
                    Intent intent = new Intent(LoginActivity.this, UserProfileActivity.class);
                    intent.putExtra("account",account);
                    startActivity(intent);
                    LoginActivity.this.finish();
                } else {
                    Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_LONG).show();
                }
            } else {
                Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_LONG).show();
            }
        } else if (view.getId() == R.id.to_register) {
            Intent intent = new Intent(this, RegisterActivity.class);

            startActivityForResult(intent, REQUEST_CODE_REGISTER);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_REGISTER && resultCode == 0 && data!=null){
            Bundle extras = data.getExtras();
            String account = extras.getString("account", "");
            String password = extras.getString("password", "");

            etAccount.setText(account);
            etPassword.setText(password);

            userName = account;
            pass = password;
        }
    }

    private void initView(){
        btnLogin = findViewById(R.id.btn_Login);
        btnLogin.setOnClickListener(this);

        etAccount = findViewById(R.id.et_account);
        etAccount.setOnClickListener(this);

        etPassword = findViewById(R.id.et_password);
        etPassword.setOnClickListener(this);

        cbRemember = findViewById(R.id.cb_remember);
        cbRemember.setOnCheckedChangeListener(this::onCheckedChangedResult);
        cbAutoLogin = findViewById(R.id.cb_auto_login);
        cbAutoLogin.setOnCheckedChangeListener(this::onCheckedChanged);

        toRegister = findViewById(R.id.to_register);
        toRegister.setOnClickListener(this);
    }
    private void initData() {
        SharedPreferences spf = getSharedPreferences("spfRecord",MODE_PRIVATE);
        boolean isRemember = spf.getBoolean("isRemember",false);
        boolean isLogin = spf.getBoolean("isLogin",false);

        String account = spf.getString("account","");
        String password = spf.getString("password","");

        if (isLogin){
            Intent intent = new Intent(LoginActivity.this, UserProfileActivity.class);
            intent.putExtra("account",account);
            startActivity(intent);
            LoginActivity.this.finish();
        }


        userName = account;
        pass = password;

        if (isRemember){
            etAccount.setText(account);
            etPassword.setText(password);
            cbRemember.setChecked(true);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b){
            cbRemember.setChecked(true);
        }
    }

    public void onCheckedChangedResult(CompoundButton compoundButton, boolean b) {
        if (!b){
            cbAutoLogin.setChecked(false);
        }
    }
}
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".UserProfileActivity">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:background="@color/colorPrimary">

            <ImageView
                android:id="@+id/iv_avatar"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerInParent="true"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/tv_nick_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/iv_avatar"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp"
                android:text="admin"
                android:textColor="@color/white"
                android:textSize="14sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tv_nick_name"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tv_gender"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/iv_avatar"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"
                    android:text="男"
                    android:textColor="@color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/tv_age"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/iv_avatar"
                    android:layout_centerHorizontal="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:text="21岁"
                    android:textColor="@color/white"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/tv_city"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/iv_avatar"
                    android:layout_centerHorizontal="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:text="北京"
                    android:textColor="@color/white"
                    android:textSize="14sp" />
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/tv_account"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_account_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/tv_account"
                android:layout_toRightOf="@id/tv_account"
                android:gravity="center"
                android:text="admin"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/tv_birth_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="出生时间"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_birth_time_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@id/tv_birth_time"
                android:layout_toRightOf="@id/tv_birth_time"
                android:gravity="center"
                android:text="123234"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/tv_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="城市"
                android:textSize="20sp"
                tools:ignore="InvalidId" />

            <TextView
                android:id="@+id/tv_home_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@id/tv_home"
                android:layout_toRightOf="@id/tv_home"
                android:gravity="center"
                android:text="北京"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/tv_school"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="学校"
                android:textSize="20sp"
                tools:ignore="InvalidId" />

            <TextView
                android:id="@+id/tv_school_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@id/tv_school"
                android:layout_toRightOf="@id/tv_school"
                android:gravity="center"
                android:text="北京大学"
                android:textSize="20sp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginTop="10dp"
            android:gravity="center_vertical"
            android:paddingLeft="10dp">

            <TextView
                android:id="@+id/tv_sign"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="个人签名"
                android:textSize="20sp"
                tools:ignore="InvalidId" />

            <TextView
                android:id="@+id/tv_sign_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@id/tv_sign"
                android:layout_toRightOf="@id/tv_sign"
                android:gravity="center"
                android:text="这个人没有设置任何签名"
                android:textSize="14sp" />
        </RelativeLayout>

        <Button
            android:id="@+id/btn_toEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/btn_bg_selector"
            android:text="编辑资料" />

        <Button
            android:id="@+id/btn_logout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/btn_bg_selector"
            android:text="退出登录" />
    </LinearLayout>
</ScrollView>
java 复制代码
package com.example.logintest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class UserProfileActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tvNickName,tvAccount,tvAge,tvGender,tvCity,tvHome,tvSchool,tvSign,tvBirthdayTime;
    private String birthDayTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);

        Button btn_toEdit = findViewById(R.id.btn_toEdit);
        btn_toEdit.setOnClickListener(this);
        Button btn_logout = findViewById(R.id.btn_logout);
        btn_logout.setOnClickListener(this);

        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }


    private void initData() {
        getDataFromspf();
    }

    private void getDataFromspf() {
        SharedPreferences spfRecord = getSharedPreferences("spfRecord",MODE_PRIVATE);
        String account = spfRecord.getString("account","");
        String nick_name = spfRecord.getString("nick_name","");
        String city = spfRecord.getString("city","");
        String gender = spfRecord.getString("gender","");
        String school = spfRecord.getString("school","");
        String birth_day_time = spfRecord.getString("birth_day_time","");
        String sign = spfRecord.getString("sign","");
        String home = spfRecord.getString("home","");
        String age = getAgeByBirthDay(birthDayTime);

        tvAccount.setText(account);
        tvNickName.setText(nick_name);
        tvAge.setText(age);
        tvHome.setText(home);
        tvSchool.setText(school);
        tvSign.setText(sign);
        tvBirthdayTime.setText(birth_day_time);
        tvGender.setText(gender);
        tvCity.setText(city);
    }

    private String getAgeByBirthDay(String birthDayTime) {
        if (TextUtils.isEmpty(birthDayTime)){
            return "";
        }
        try {
            int index = birthDayTime.indexOf("年");
            String result = birthDayTime.substring(0,index);

            int parseInt = Integer.parseInt(result);
            return String.valueOf(2021-parseInt);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    private void initView() {
        tvAccount = findViewById(R.id.tv_account_text);
        tvNickName = findViewById(R.id.tv_nick_name);
        tvAge = findViewById(R.id.tv_age);
        tvHome = findViewById(R.id.tv_home_text);
        tvSchool = findViewById(R.id.tv_school_text);
        tvSign = findViewById(R.id.tv_sign_text);
        tvBirthdayTime = findViewById(R.id.tv_birth_time_text);
        tvGender = findViewById(R.id.tv_gender);
        tvCity = findViewById(R.id.tv_city);

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_toEdit){
            Intent intent = new Intent(this, EditProfileActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.btn_logout) {
            SharedPreferences spf = getSharedPreferences("spfRecord",MODE_PRIVATE);
            SharedPreferences.Editor edit = spf.edit();
            edit.putBoolean("isLogin",false);
            edit.apply();

            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            this.finish();
        }
    }
}
XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".EditProfileActivity">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="250dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:text="更换头像"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/iv_avatar"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_centerInParent="true"
                android:src="@mipmap/ic_launcher" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/iv_avatar"
                android:layout_marginTop="5dp"
                android:gravity="center"
                android:orientation="horizontal">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/btn_bg_selector"
                    android:text="拍照" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:background="@drawable/btn_bg_selector"
                    android:text="相册" />
            </LinearLayout>

        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_account"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="账号:"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/et_account_text"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/edit_text_bg"
                android:hint="请输入你的账号"
                android:paddingLeft="5dp"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_nick_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="昵称:"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/et_nick_name_text"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/edit_text_bg"
                android:hint="请输入你的账号"
                android:paddingLeft="5dp"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别"
                android:textSize="20sp"/>
            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center">
                <RadioButton
                    android:id="@+id/rb_boy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="男"/>
                <RadioButton
                    android:id="@+id/rb_girl"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="女"
                    android:layout_marginLeft="10dp"/>
            </RadioGroup>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_birth_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="出生日期:"
                android:textSize="25sp" />

            <TextView
                android:id="@+id/tv_birth_time_text"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:text="1998年3月23 15点25分"
                android:paddingLeft="5dp"
                android:textSize="18sp" />
            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center_vertical"
                android:src="@drawable/baseline_arrow_forward_24"
                android:layout_marginLeft="30dp"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="城市:"
                android:textSize="25sp" />

            <androidx.appcompat.widget.AppCompatSpinner
                android:id="@+id/sp_city"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/edit_text_bg"
                android:entries="@array/cities"
                android:spinnerMode="dropdown"
                android:paddingLeft="5dp"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_school"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="学校:"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/et_school_text"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:background="@drawable/edit_text_bg"
                android:hint="请输入你的学校"
                android:paddingLeft="5dp"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_sign"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="个人签名:"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/et_sign_text"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:background="@drawable/edit_text_bg"
                android:hint="请设置你的个人签名"
                android:textSize="18sp" />
        </LinearLayout>


        <Button
            android:id="@+id/btn_save"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/btn_bg_selector"
            android:text="保存" />

    </LinearLayout>
</ScrollView>
java 复制代码
package com.example.logintest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.TimePicker;

public class EditProfileActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText etNickName,etAccount,etSchool,etSign;
    private  TextView tvBirthDayTime;
    private RadioButton rbBoy,rbGirl;
    private AppCompatSpinner spinnerCity;
    private String[] cities;
    private int selectedCityPosition;
    private String selectedCity;
    private String birthDay;
    private String birthDayTime;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);
        Button btn_save = findViewById(R.id.btn_save);
        btn_save.setOnClickListener(this);
        
        initView();
        initData();
        
        initEvent();
    }

    private void initView() {
        etAccount = findViewById(R.id.et_account_text);
        etNickName = findViewById(R.id.et_nick_name_text);
        etSchool = findViewById(R.id.et_school_text);
        etSign = findViewById(R.id.et_sign_text);

        tvBirthDayTime = findViewById(R.id.tv_birth_time_text);
        rbBoy = findViewById(R.id.rb_boy);
        rbGirl = findViewById(R.id.rb_girl);
        spinnerCity = findViewById(R.id.sp_city);
    }
    private void initData() {
        cities = getResources().getStringArray(R.array.cities);

        getDataFromspf();
    }
    private void getDataFromspf() {
        SharedPreferences spfRecord = getSharedPreferences("spfRecord",MODE_PRIVATE);
        String account = spfRecord.getString("account","");
        String nick_name = spfRecord.getString("nick_name","");
        String age = spfRecord.getString("age","");
        String city = spfRecord.getString("city","");
        String gender = spfRecord.getString("gender","");
        String school = spfRecord.getString("school","");
        String birth_day_time = spfRecord.getString("birth_day_time","");
        String sign = spfRecord.getString("sign","");
        String home = spfRecord.getString("home","");

        etAccount.setText(account);
        etNickName.setText(nick_name);
        etSchool.setText(age);
        etSign.setText(home);
        tvBirthDayTime.setText(birthDayTime);

        if (TextUtils.equals("男",gender)){
            rbBoy.setChecked(true);
        }

        if (TextUtils.equals("女",gender)){
            rbGirl.setChecked(true);
        }

        for (int i = 0; i < cities.length; i++) {
            if (TextUtils.equals(cities[i],city)){
                selectedCityPosition = i;
                break;
            }
        }

        spinnerCity.setSelection(selectedCityPosition);
    }

    private void initEvent() {
        spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectedCityPosition = i;
                selectedCity = cities[i];
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        tvBirthDayTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new DatePickerDialog(EditProfileActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        int realMonth = i1+1;
                        birthDay = i+"年"+realMonth+"月"+i2+"日";

                        popTimePick();
                    }

                },2024,2,17).show();
            }
        });
    }
    private void popTimePick() {
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int i, int i1) {
                birthDayTime = birthDay+i+"时"+i1+"分";
                tvBirthDayTime.setText(birthDayTime);
            }
        },12,36,true).show();
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_save){
            String account = etAccount.getText().toString();
            String sign = etSign.getText().toString();
            String school = etSchool.getText().toString();
            String nickName = etNickName.getText().toString();

            String gender = "男";
            if (rbBoy.isChecked()){
                gender = "男";
            }
            if (rbGirl.isChecked()){
                gender = "女";
            }

            SharedPreferences spfRecord = getSharedPreferences("spfRecord",MODE_PRIVATE);
            SharedPreferences.Editor editor = spfRecord.edit();
            editor.putString("account",account);
            editor.putString("sign",sign);
            editor.putString("school",school);
            editor.putString("nick_name",nickName);
            editor.putString("birth_day_time",birthDayTime);
            editor.putString("city",selectedCity);
            editor.putString("gender",gender);
            editor.apply();

            this.finish();
        }
    }
}
相关推荐
Dnelic-2 小时前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
Eastsea.Chen4 小时前
MTK Android12 user版本MtkLogger
android·framework
长亭外的少年12 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿14 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神15 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛16 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法16 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
NotesChapter17 小时前
Android吸顶效果,并有着ViewPager左右切换
android
_祝你今天愉快18 小时前
分析android :The binary version of its metadata is 1.8.0, expected version is 1.5.
android
暮志未晚Webgl19 小时前
109. UE5 GAS RPG 实现检查点的存档功能
android·java·ue5