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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><applicationandroid: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"><activityandroid:name=".EditProfileActivity"android:exported="false" /><activityandroid:name=".UserProfileActivity"android:exported="false" /><activityandroid: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><activityandroid:name=".RegisterActivity"android:exported="false"android:label="注册" /><activityandroid:name=".MainActivity"android:exported="false"android:label="首页" /></application></manifest>

<?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>

<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 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>

<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 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 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 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"><TextViewandroid: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"/><Buttonandroid: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>

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;@Overrideprotected 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);}@Overridepublic 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 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"><LinearLayoutandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="账        号:"android:textSize="25sp"/><EditTextandroid: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><LinearLayoutandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密        码:"android:textSize="25sp"/><EditTextandroid: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><LinearLayoutandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="确认密码:"android:textSize="25sp"/><EditTextandroid: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><Buttonandroid: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"/><CheckBoxandroid: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>

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;@Overrideprotected 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);}@Overridepublic 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 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"><LinearLayoutandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="账号:"android:textSize="25sp"/><EditTextandroid: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><LinearLayoutandroid: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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码:"android:textSize="25sp"/><EditTextandroid: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><LinearLayoutandroid: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"><CheckBoxandroid:id="@+id/cb_remember"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="记住密码"/><CheckBoxandroid: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><Buttonandroid: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"/><TextViewandroid: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>

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";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);initView();initData();}@Overridepublic 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);}}@Overrideprotected 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);}}@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {if (b){cbRemember.setChecked(true);}}public void onCheckedChangedResult(CompoundButton compoundButton, boolean b) {if (!b){cbAutoLogin.setChecked(false);}}
}

<?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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="250dp"android:background="@color/colorPrimary"><ImageViewandroid:id="@+id/iv_avatar"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerInParent="true"android:src="@mipmap/ic_launcher" /><TextViewandroid: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" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/tv_nick_name"android:gravity="center"android:orientation="horizontal"><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="10dp"android:gravity="center_vertical"android:paddingLeft="10dp"><TextViewandroid:id="@+id/tv_account"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="账号"android:textSize="20sp" /><TextViewandroid: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><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="10dp"android:gravity="center_vertical"android:paddingLeft="10dp"><TextViewandroid:id="@+id/tv_birth_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="出生时间"android:textSize="20sp" /><TextViewandroid: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><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="10dp"android:gravity="center_vertical"android:paddingLeft="10dp"><TextViewandroid:id="@+id/tv_home"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="城市"android:textSize="20sp"tools:ignore="InvalidId" /><TextViewandroid: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><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="10dp"android:gravity="center_vertical"android:paddingLeft="10dp"><TextViewandroid:id="@+id/tv_school"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="学校"android:textSize="20sp"tools:ignore="InvalidId" /><TextViewandroid: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><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="40dp"android:layout_marginTop="10dp"android:gravity="center_vertical"android:paddingLeft="10dp"><TextViewandroid:id="@+id/tv_sign"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="个人签名"android:textSize="20sp"tools:ignore="InvalidId" /><TextViewandroid: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><Buttonandroid: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="编辑资料" /><Buttonandroid: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>

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;@Overrideprotected 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();}@Overrideprotected 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);}@Overridepublic 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 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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="250dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dp"android:text="更换头像"android:textSize="20sp" /><ImageViewandroid:id="@+id/iv_avatar"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerInParent="true"android:src="@mipmap/ic_launcher" /><LinearLayoutandroid: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"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/btn_bg_selector"android:text="拍照" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:background="@drawable/btn_bg_selector"android:text="相册" /></LinearLayout></RelativeLayout><LinearLayoutandroid: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"><TextViewandroid:id="@+id/tv_account"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="账号:"android:textSize="25sp" /><EditTextandroid: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><LinearLayoutandroid: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"><TextViewandroid:id="@+id/tv_nick_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="昵称:"android:textSize="25sp" /><EditTextandroid: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><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:gravity="center_vertical"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="性别"android:textSize="20sp"/><RadioGroupandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><RadioButtonandroid:id="@+id/rb_boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"/><RadioButtonandroid:id="@+id/rb_girl"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"android:layout_marginLeft="10dp"/></RadioGroup></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginTop="10dp"android:layout_marginRight="10dp"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_birth_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="出生日期:"android:textSize="25sp" /><TextViewandroid: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" /><ImageViewandroid:layout_width="20dp"android:layout_height="20dp"android:layout_gravity="center_vertical"android:src="@drawable/baseline_arrow_forward_24"android:layout_marginLeft="30dp"/></LinearLayout><LinearLayoutandroid: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"><TextViewandroid:id="@+id/tv_home"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="城市:"android:textSize="25sp" /><androidx.appcompat.widget.AppCompatSpinnerandroid: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><LinearLayoutandroid: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"><TextViewandroid:id="@+id/tv_school"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="学校:"android:textSize="25sp" /><EditTextandroid: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><LinearLayoutandroid: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"><TextViewandroid:id="@+id/tv_sign"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="个人签名:"android:textSize="25sp" /><EditTextandroid: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><Buttonandroid: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>

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;@Overrideprotected 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() {@Overridepublic void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {selectedCityPosition = i;selectedCity = cities[i];}@Overridepublic void onNothingSelected(AdapterView<?> adapterView) {}});tvBirthDayTime.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {new DatePickerDialog(EditProfileActivity.this, new DatePickerDialog.OnDateSetListener() {@Overridepublic 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() {@Overridepublic void onTimeSet(TimePicker timePicker, int i, int i1) {birthDayTime = birthDay+i+"时"+i1+"分";tvBirthDayTime.setText(birthDayTime);}},12,36,true).show();}@Overridepublic 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();}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/413102.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[Linux 进程(五)] 程序地址空间深度剖析

文章目录 1、前言2、什么是进程地址空间&#xff1f;3、进程地址空间的划分4、虚拟地址与物理地址的关系5、页表的作用扩展 6、为什么要有地址空间&#xff1f; 1、前言 Linux学习路线比较线性&#xff0c;也比较长&#xff0c;因此一个完整的知识点学习就会分布在两篇文章中&…

设计模式入门

0. 类图 1. 设计原则 1.单一职责原则&#xff1a;每个类只有一个功能 2.开放封闭原则&#xff1a;模块和函数应该对扩展开放(对提供方)&#xff0c;对修改关闭(对使用方) 3.里氏代换原则&#xff1a;子类拥有父类的所有方法和属性&#xff0c;从而可以减少创建类的工作量 4.依…

黑马程序员 Java设计模式学习笔记(一)

目录 一、设计模式概述 1.1、23种设计模式有哪些&#xff1f; 1.2、软件设计模式的概念 1.3、学习设计模式的必要性 1.4、设计模式分类 二、UML图 2.1、类图概述 2.2、类图的作用 2.3、类图表示法 类的表示方式 类与类之间关系的表示方式 关联关系 聚合关系 组合…

GLM-4 能力接近ChatGPT4和Claude 2.1

本文以GLM-4 发布功能作为基准对比ChatGPT4&#xff0c;Claude-2测试。 帮关注下 输入测试用例是GLM-4提供&#xff0c;用专业性打败专业性才有趣&#xff01; 以多模态理解&#xff0c;代码解释器&#xff0c;工具调用&#xff0c;逻辑推理方向测试。 1月16日&#xff0c;…

提纲框架写作方法

论文提纲 论文提纲的意义 有利于检查构思有利于调整修改和写作 拟定提纲的目的 拟标题写总论点做总安排&#xff1a;几个方面&#xff0c;什么顺序做下位论点&#xff1a;每个项目的下位论点&#xff0c;直到段一级&#xff0c;写段的论点句考虑各段安排&#xff0c;把材料…

新火种AI|GPT-5前瞻!GPT-5将具备哪些新能力?

作者&#xff1a;小岩 编辑&#xff1a;彩云 Sam Altman在整个AI领域&#xff0c;乃至整个科技领域都被看作是极具影响力的存在&#xff0c;而2023年OpenAI无限反转的宫斗事件更是让Sam Altman刷足了存在感&#xff0c;他甚至被《时代》杂志评为“2023年度CEO”。 也正因此&…

01 SpringMVC的快速理解

1.1 如图所示&#xff0c;SpringMVC负责表述层&#xff08;控制层Controller&#xff09;实现简化&#xff01; SpringMVC的作用主要覆盖的是表述层&#xff0c;例如&#xff1a; 请求映射、数据输入、视图界面、请求分发、表单回显、会话控制、过滤拦截、异步交互、文件上传…

CSS 网页布局

文章目录 布局元素页眉标签导航栏侧边栏网页主体文章页脚测试网页布局元素 布局元素 在html4.01等之前的版本中html并没有专用的布局元素&#xff0c;而是统统使用div进行布局 页眉标签 header标签一般用来嵌套网页的标题&#xff0c;搜索栏&#xff0c;导航栏等 导航栏 n…

redis原理(四)redis命令

目录 一、字符串命令&#xff1a; 二、列表命令&#xff1a; 三、集合命令&#xff1a; 四、散列命令&#xff1a; 五、有序集合命令&#xff1a; 六、redis发布与订阅命令&#xff1a; 七、事务命令 八、其他命令 1、排序&#xff1a;SORT 2、键的过期时间&#xff…

使用muduo库编写网络server端

muduo库源码编译安装和环境搭建 C muduo网络库知识分享01 - Linux平台下muduo网络库源码编译安装-CSDN博客 #include<iostream> #include<muduo/net/TcpServer.h> #include<muduo/net/EventLoop.h> using namespace std; using namespace muduo; using name…

STC8H8K蓝牙智能巡线小车——1. 环境搭建(基于RTX51操作系统)

1. 基本介绍 开发环境准备&#xff1a;Keil uVision5 烧录软件&#xff1a;STC-ISP&#xff08;V6.92A&#xff09; 芯片&#xff1a; STC8H8K64U-45I-LQFP64 芯片引脚&#xff1a; 2.创建项目 打开Keil&#xff0c;点击【Project】&#xff0c;选择【new uVersion proje…

中北数据结构2023真题

雪雾: 设计一个算法&#xff0c;将一个节点值为自然数的单列表拆分成两个单列表&#xff0c;原表中值为偶数的节点保留&#xff0c;而值为奇数的节点&#xff0c;按他们在原表的相对次序组成一个新的单列表 #include <stdio.h> #include <stdlib.h>typedef struct…