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();}}
}
相关文章:

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"an…...

[剪藏] - 任泽平年终演讲精华:点燃希望——2024中国经济十大预测
任泽平年终演讲精华:点燃希望——2024中国经济十大预测 泽平宏观 2023-12-23 08:01 发表于上海 12月22日22:30,任泽平年终秀“点燃希望乐观者前行——2024中国经济十大预测”圆满收官。 泽平宏观、北京广播电视台、上海高净值研究院、北京时间等携手打…...

基于LabVIEW的压力传感器测试系统
摘要 现在各类压力传感器已广泛应用于各种工业自控环境,对压力传感器的研究 及应用,既可以体现一个国家的科技发展水平,又可以提升国家的综合国力,还 可以在丰富、方便和智能化人们的生活方面做出重要的贡献。而针对不同仪器组 成…...
Vue 如何使用WebSocket与服务器建立链接 持续保持通信
WebSocket 浏览器通过JavaScript向服务器发出建立WebSocket链接的请求,链接建立后,客户端和服务器端就可以通过TCP链接直接交互数据。WebSocket链接后可以通过send()方法来向服务器发送数据,并通过onnessage事件来接受服务器返回的数据。 创…...
自动驾驶模拟器
目录 Carla 自动驾驶模拟器 Udacity自动驾驶模拟器 Carla 自动驾驶模拟器 pip install carla 需要下载地图 Udacity自动驾驶模拟器...

Jmeter的文件参数化:CSV数据文件设置和_CSVRead函数
一、CSV数据文件设置 1、简介 CSV数据文件配置(CSV Data Set Config)可以将CSV文件中数据读入自定义变量中 Jmeter中CSV数据文件配置的界面如下图所示: 其中: (1)文件编码 文件的编码格式,与所…...

windows编译TensorFlowServing
概述 整个编译打包过程的总体思路,是参照在linux下的编译流程,配置环境,执行编译命令,根据编译器/链接器反馈的错误,修改相应的源码或者相关库文件的存放路径,编译出windows平台下静态库和二进制执行文件。…...
debian 12 安装 浏览器 Epiphany
Epiphany 什么epiphany-browser epiphany-browser 是: Epiphany 是一款简单而强大的 GNOME 网络浏览器,针对 非技术用户。它的原则是简单和标准 合规。 简单性是通过精心设计的用户界面和依赖来实现的 在用于执行外部任务(如阅读 电子邮件…...

Kafka-消费者-KafkaConsumer分析
与KafkaProducer不同的是,KafkaConsumer不是一个线程安全的类。 为了便于分析,我们认为下面介绍的所有操作都是在同一线程中完成的,所以不需要考虑锁的问题。 这种设计将实现多线程处理消息的逻辑转移到了调用KafkaConsumer的代码中&#x…...

Spring | Spring中的Bean--下
Spring中的Bean: 4.Bean的生命周期5.Bean的配装配式 ( 添加Bean到IOC容器的方式 依赖注入的方式 )5.1 基于XML的配置5.2 基于Annotation (注解) 的装配 (更常用)5.3 自动装配 4.Bean的生命周期 Spring容器可以管理 singleton作用域的Bean的生命周期,在此…...

本周五上海见 第二届证券基金行业先进计算技术大会暨2024低时延技术创新实践论坛(上海站)即将召开
低时延技术是证券基金期货领域业务系统的核心技术,是打造极速交易系统领先优势的关键,也是证券基金行业关注的前沿技术热点。 1月19日下午,第二届证券基金行业先进计算技术大会暨2024低时延技术创新实践论坛(上海站)即…...

怎么安装IK分词器
.安装IK分词器 1.在线安装ik插件(较慢) # 进入容器内部 docker exec -it elasticsearch /bin/bash # 在线下载并安装 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elastics…...

【踩坑】flask_uploads报错cannot import name ‘secure_filename‘
转载请注明出处:小锋学长生活大爆炸[xfxuezhang.cn] 背景说明 截至目前,用新版的flask实现文件上传(用到flask_uploads库),会出现这个问题。 问题原因 版本问题,新的werkzeug已经把secure_filename的位置改了。 解决方法 手动修改…...

AI编程可视化Java项目拆解第一弹,解析本地Java项目
之前分享过一篇使用 AI 可视化 Java 项目的文章,同步在 AI 破局星球、知乎、掘金等地方都分享了。 原文在这里AI 编程:可视化 Java 项目 有很多人感兴趣,我打算写一个系列文章拆解这个项目,大家多多点赞支持~ 今天分享的是第一…...

使用arcgis pro是类似的控件样式 WPF
1.资源加载 <controls:ProWindow.Resources><ResourceDictionary><ResourceDictionary.MergedDictionaries><extensions:DesignOnlyResourceDictionary Source"pack://application:,,,/ArcGIS.Desktop.Framework;component\Themes\Default.xaml&quo…...
C语言所有字符串函数举例如何使用
strcpy: 将一个字符串复制到另一个字符串中 char source[] "Hello"; char destination[10]; strcpy(destination, source);strcat: 将一个字符串连接到另一个字符串的末尾 char str1[20] "Hello"; char str2[] "World"; strcat(str1, str2)…...

ArcGIS Pro 如何新建布局
你是否已经习惯了在ArcGIS中数据视图和布局视图之间来回切换,到了ArcGIS Pro中却找不到二者之间切换的按钮,即使新建布局后却发现地图怎么却是一片空白。 这一切的一切都是因为ArcGIS Pro的功能框架完全不同,这里为大家介绍一下在ArcGIS Pro…...
如何解决态势感知中的“时隐时现”问题
解决态势感知中的“时隐时现”问题有以下几个方法: 1、确保所有关键的监控设备和传感器正常运行,能够及时和准确地检测到各种异常情况。 2、引入先进的技术手段。例如使用人工智能和机器学习算法来识别和分析大量的数据,快速发现异常和威胁&a…...

为什么JavaScript中0.1 + 0.2 ≠ 0.3
JavaScript中的浮点数运算有时候会出现一点偏差。下面解释为什么0.1 0.2 ≠ 0.3,以及如果你需要精确运算应该怎么做。 如果1 2 3,那么为什么在JavaScript中0.1 0.2 ≠ 0.3?这个原因与计算机科学和浮点数运算有关。 我建议你打开浏览器的控制台,输入0.1 0.2来查看结果。…...

Unity关于纹理图片格式带来的内存问题和对预制体批量格式和大小减半处理
我们经常会遇到内存问题,这次就是遇到很多图片的默认格式被改成了RGB32,导致Android打包后运行内存明显增加。 发生了什么 打包Android后,发现经常崩溃,明显内存可能除了问题,看了内存后发现了问题。 见下图…...

wordpress后台更新后 前端没变化的解决方法
使用siteground主机的wordpress网站,会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后,网站没有变化的情况。 不熟悉siteground主机的新手,遇到这个问题,就很抓狂,明明是哪都没操作错误&#x…...
Spring Boot 实现流式响应(兼容 2.7.x)
在实际开发中,我们可能会遇到一些流式数据处理的场景,比如接收来自上游接口的 Server-Sent Events(SSE) 或 流式 JSON 内容,并将其原样中转给前端页面或客户端。这种情况下,传统的 RestTemplate 缓存机制会…...
day52 ResNet18 CBAM
在深度学习的旅程中,我们不断探索如何提升模型的性能。今天,我将分享我在 ResNet18 模型中插入 CBAM(Convolutional Block Attention Module)模块,并采用分阶段微调策略的实践过程。通过这个过程,我不仅提升…...

.Net框架,除了EF还有很多很多......
文章目录 1. 引言2. Dapper2.1 概述与设计原理2.2 核心功能与代码示例基本查询多映射查询存储过程调用 2.3 性能优化原理2.4 适用场景 3. NHibernate3.1 概述与架构设计3.2 映射配置示例Fluent映射XML映射 3.3 查询示例HQL查询Criteria APILINQ提供程序 3.4 高级特性3.5 适用场…...

MongoDB学习和应用(高效的非关系型数据库)
一丶 MongoDB简介 对于社交类软件的功能,我们需要对它的功能特点进行分析: 数据量会随着用户数增大而增大读多写少价值较低非好友看不到其动态信息地理位置的查询… 针对以上特点进行分析各大存储工具: mysql:关系型数据库&am…...
【Java学习笔记】Arrays类
Arrays 类 1. 导入包:import java.util.Arrays 2. 常用方法一览表 方法描述Arrays.toString()返回数组的字符串形式Arrays.sort()排序(自然排序和定制排序)Arrays.binarySearch()通过二分搜索法进行查找(前提:数组是…...
工程地质软件市场:发展现状、趋势与策略建议
一、引言 在工程建设领域,准确把握地质条件是确保项目顺利推进和安全运营的关键。工程地质软件作为处理、分析、模拟和展示工程地质数据的重要工具,正发挥着日益重要的作用。它凭借强大的数据处理能力、三维建模功能、空间分析工具和可视化展示手段&…...
Neo4j 集群管理:原理、技术与最佳实践深度解析
Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...
代理篇12|深入理解 Vite中的Proxy接口代理配置
在前端开发中,常常会遇到 跨域请求接口 的情况。为了解决这个问题,Vite 和 Webpack 都提供了 proxy 代理功能,用于将本地开发请求转发到后端服务器。 什么是代理(proxy)? 代理是在开发过程中,前端项目通过开发服务器,将指定的请求“转发”到真实的后端服务器,从而绕…...
Typeerror: cannot read properties of undefined (reading ‘XXX‘)
最近需要在离线机器上运行软件,所以得把软件用docker打包起来,大部分功能都没问题,出了一个奇怪的事情。同样的代码,在本机上用vscode可以运行起来,但是打包之后在docker里出现了问题。使用的是dialog组件,…...