将以上问题代码显示出来
时间: 2024-03-06 09:48:32 浏览: 15
好的,以下是这个app的代码实现,包括四个Activity的代码和布局文件的代码。
1. LoginActivity.java
```
public class LoginActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
private Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
btnRegister = findViewById(R.id.btnRegister);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
// 判断账号密码是否正确
if (username.equals("admin") && password.equals("123456")) {
// 跳转到InfoActivity
Intent intent = new Intent(LoginActivity.this, InfoActivity.class);
startActivity(intent);
} else {
// 提示账号或密码错误
Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到RegisterActivity
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
```
2. activity_login.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
```
3. RegisterActivity.java
```
public class RegisterActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
btnRegister = findViewById(R.id.btnRegister);
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
// 保存账号密码
SharedPreferences preferences = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.apply();
// 跳转回LoginActivity
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
}
});
}
}
```
4. activity_register.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/btnRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
```
5. InfoActivity.java
```
public class InfoActivity extends AppCompatActivity {
private EditText etStudentId;
private EditText etMajor;
private EditText etDepartment;
private EditText etPhoneNumber;
private Button btnSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
etStudentId = findViewById(R.id.etStudentId);
etMajor = findViewById(R.id.etMajor);
etDepartment = findViewById(R.id.etDepartment);
etPhoneNumber = findViewById(R.id.etPhoneNumber);
btnSave = findViewById(R.id.btnSave);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String studentId = etStudentId.getText().toString();
String major = etMajor.getText().toString();
String department = etDepartment.getText().toString();
String phoneNumber = etPhoneNumber.getText().toString();
// 跳转到DisplayActivity,并传递输入的信息
Intent intent = new Intent(InfoActivity.this, DisplayActivity.class);
intent.putExtra("studentId", studentId);
intent.putExtra("major", major);
intent.putExtra("department", department);
intent.putExtra("phoneNumber", phoneNumber);
startActivity(intent);
}
});
}
}
```
6. activity_info.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etStudentId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学号" />
<EditText
android:id="@+id/etMajor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入专业" />
<EditText
android:id="@+id/etDepartment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入院系" />
<EditText
android:id="@+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号码" />
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存" />
</LinearLayout>
```
7. DisplayActivity.java
```
public class DisplayActivity extends AppCompatActivity {
private TextView tvStudentId;
private TextView tvMajor;
private TextView tvDepartment;
private TextView tvPhoneNumber;
private Button btnBackToInfo;
private Button btnBackToLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
tvStudentId = findViewById(R.id.tvStudentId);
tvMajor = findViewById(R.id.tvMajor);
tvDepartment = findViewById(R.id.tvDepartment);
tvPhoneNumber = findViewById(R.id.tvPhoneNumber);
btnBackToInfo = findViewById(R.id.btnBackToInfo);
btnBackToLogin = findViewById(R.id.btnBackToLogin);
// 获取传递的信息
Intent intent = getIntent();
String studentId = intent.getStringExtra("studentId");
String major = intent.getStringExtra("major");
String department = intent.getStringExtra("department");
String phoneNumber = intent.getStringExtra("phoneNumber");
// 显示信息
tvStudentId.setText(studentId);
tvMajor.setText(major);
tvDepartment.setText(department);
tvPhoneNumber.setText(phoneNumber);
btnBackToInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转回InfoActivity
Intent intent = new Intent(DisplayActivity.this, InfoActivity.class);
startActivity(intent);
}
});
btnBackToLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转回LoginActivity
Intent intent = new Intent(DisplayActivity.this, LoginActivity.class);
startActivity(intent);
}
});
}
}
```
8. activity_display.xml
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tvStudentId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvMajor"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvDepartment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnBackToInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回个人信息" />
<Button
android:id="@+id/btnBackToLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回登录界面" />
</LinearLayout>
```