Android代码12月23日提交

This commit is contained in:
Levi 2024-12-23 13:06:29 +08:00
parent ed78b6e686
commit 85079887d6
7 changed files with 390 additions and 1 deletions

View File

@ -15,6 +15,9 @@
android:theme="@style/Theme.CourseDesign" android:theme="@style/Theme.CourseDesign"
android:usesCleartextTraffic="true" android:usesCleartextTraffic="true"
tools:targetApi="31"> tools:targetApi="31">
<activity
android:name=".MyInformationActivity"
android:exported="false" />
<activity <activity
android:name=".UpdateCommodityMainActivity" android:name=".UpdateCommodityMainActivity"
android:exported="false" /> android:exported="false" />

View File

@ -20,6 +20,14 @@ public interface Api {
@GET("/system/person/login") @GET("/system/person/login")
Call<LoginResult> login(@Header("Authorization") String token, @Query("phone") String username, @Query("password") String password); Call<LoginResult> login(@Header("Authorization") String token, @Query("phone") String username, @Query("password") String password);
//http://127.0.0.1:8080/system/person
@POST("/system/person")
Call<Result> addPerson(@Header("Authorization") String token, @Body LoginResult.DataDTO dataDTO);
//http://127.0.0.1:8080/system/person
@PUT("/system/person")
Call<Result> updatePerson(@Header("Authorization") String token, @Body LoginResult.DataDTO dataDTO);
//http://127.0.0.1:8080/system/commodity/list //http://127.0.0.1:8080/system/commodity/list
@GET("/system/commodity/list2") @GET("/system/commodity/list2")
Call<CommodityListResult> getCommodityList(@Header("Authorization") String token, @Query("state") String state); Call<CommodityListResult> getCommodityList(@Header("Authorization") String token, @Query("state") String state);

View File

@ -46,6 +46,14 @@ public class MineFragment extends Fragment {
phone.setText(sharedPreferences.getString("phone", "")); phone.setText(sharedPreferences.getString("phone", ""));
Glide.with(getContext()).load(sharedPreferences.getString("picture", "")).into(picture); Glide.with(getContext()).load(sharedPreferences.getString("picture", "")).into(picture);
picture.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), MyInformationActivity.class));
}
});
myOrder.setOnClickListener(new View.OnClickListener() { myOrder.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {

View File

@ -0,0 +1,107 @@
package com.example.coursedesign;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MyInformationActivity extends AppCompatActivity {
EditText name,phone,password,address,major,age,gender;
Button submit;
LoginResult.DataDTO data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_my_information);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
name = findViewById(R.id.editTextText6);
phone = findViewById(R.id.editTextPhone2);
password = findViewById(R.id.editTextNumberPassword);
address = findViewById(R.id.editTextText13);
major = findViewById(R.id.editTextText14);
age = findViewById(R.id.editTextNumber2);
gender = findViewById(R.id.editTextText12);
submit = findViewById(R.id.button10);
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
int userId = sharedPreferences.getInt("userId", 0);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.138.63.204:8080/")
.addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析
.build();
Api api = retrofit.create(Api.class);
Call<LoginResult> loginCall = api.login("Bearer ", sharedPreferences.getString("phone", ""), sharedPreferences.getString("password", ""));
loginCall.enqueue(new retrofit2.Callback<LoginResult>() {
@Override
public void onResponse(Call<LoginResult> call, retrofit2.Response<LoginResult> response) {
LoginResult loginResult = response.body();
if (loginResult != null && loginResult.data != null) {
data = loginResult.data;
name.setText(loginResult.data.name.toString());
phone.setText(loginResult.data.phone.toString());
password.setText(loginResult.data.password.toString());
address.setText(loginResult.data.address.toString());
major.setText(loginResult.data.major.toString());
age.setText(loginResult.data.age.toString());
gender.setText(loginResult.data.gender.toString());
}
}
@Override
public void onFailure(Call<LoginResult> call, Throwable t) {
}
});
submit.setOnClickListener(v -> {
data.name = name.getText().toString();
data.phone = phone.getText().toString();
data.password = password.getText().toString();
data.address = address.getText().toString();
data.major = major.getText().toString();
data.age = Integer.parseInt(age.getText().toString());
data.gender = gender.getText().toString();
Call<Result> updatePersonCall = api.updatePerson("Bearer ", data);
updatePersonCall.enqueue(new retrofit2.Callback<Result>() {
@Override
public void onResponse(Call<Result> call, retrofit2.Response<Result> response) {
Result result = response.body();
if(result.code == 200){
Toast.makeText(MyInformationActivity.this, "修改个人信息成功", Toast.LENGTH_SHORT).show();
finish();
}
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.e("error", t.getMessage());
}
});
});
}
}

View File

@ -1,8 +1,10 @@
package com.example.coursedesign; package com.example.coursedesign;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge; import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
@ -10,6 +12,11 @@ import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat; import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat; import androidx.core.view.WindowInsetsCompat;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RegistrationActivity extends AppCompatActivity { public class RegistrationActivity extends AppCompatActivity {
EditText username,phone,password; EditText username,phone,password;
@ -34,7 +41,38 @@ public class RegistrationActivity extends AppCompatActivity {
// 注册按钮监听 // 注册按钮监听
register.setOnClickListener(v -> { register.setOnClickListener(v -> {
LoginResult.DataDTO dataDTO = new LoginResult.DataDTO();
dataDTO.name = username.getText().toString();
dataDTO.phone = phone.getText().toString();
dataDTO.password = password.getText().toString();
dataDTO.picture = "https://tse3-mm.cn.bing.net/th/id/OIP-C.lXFxe1bucFZmHU_GG0WbwAHaHa?w=150&h=183&c=7&r=0&o=5&pid=1.7";
//获取数据
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.138.63.204:8080/")
.addConverterFactory(GsonConverterFactory.create()) //返回结果用Gson解析
.build();
Api api = retrofit.create(Api.class);
Call<Result> addPersonCall = api.addPerson("Bearer ", dataDTO);
addPersonCall.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, retrofit2.Response<Result> response) {
Result result = response.body();
if(result.code == 200){
Toast.makeText(RegistrationActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
finish();
}else{
Toast.makeText(RegistrationActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
Log.e("error", t.getMessage());
}
});
}); });
back.setOnClickListener(v -> { back.setOnClickListener(v -> {

View File

@ -0,0 +1,225 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MyInformationActivity">
<TextView
android:id="@+id/textView44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Back"
android:textColor="@color/teal_200"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView40"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView40" />
<TextView
android:id="@+id/textView40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="修改用户信息"
android:textColor="@color/teal_200"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/shape"
app:layout_constraintBottom_toTopOf="@+id/button10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView40"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="@+id/textView47"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="手机号"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView48"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView46" />
<TextView
android:id="@+id/textView48"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="密码"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView47" />
<TextView
android:id="@+id/textView49"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="性别"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView50"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView48" />
<TextView
android:id="@+id/textView50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="年龄"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView49" />
<TextView
android:id="@+id/textView51"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="地址"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView55"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView50" />
<TextView
android:id="@+id/textView55"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="专业"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView51" />
<TextView
android:id="@+id/textView46"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="姓名"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textView47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextText6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="@+id/textView46"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView46" />
<EditText
android:id="@+id/editTextPhone2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:ems="10"
android:inputType="phone"
app:layout_constraintBottom_toBottomOf="@+id/textView47"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView47" />
<EditText
android:id="@+id/editTextNumberPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:ems="10"
android:inputType="numberPassword"
app:layout_constraintBottom_toBottomOf="@+id/textView48"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView48" />
<EditText
android:id="@+id/editTextText12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="@+id/textView49"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView49" />
<EditText
android:id="@+id/editTextNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="@+id/textView50"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView50" />
<EditText
android:id="@+id/editTextText13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="@+id/textView51"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView51" />
<EditText
android:id="@+id/editTextText14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="36dp"
android:ems="10"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="@+id/textView55"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView55"
app:layout_constraintVertical_bias="0.533" />
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="提交"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -78,7 +78,7 @@
android:background="@color/white" android:background="@color/white"
android:ems="15" android:ems="15"
android:inputType="text" android:inputType="text"
android:text="请输入用户名" android:hint="请输入用户名"
android:textColor="#7F7F7F" android:textColor="#7F7F7F"
android:textSize="24sp" android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"