用AndroidStudio编写一个购物小程序,要求实现三个页面功能:登录注册页面、首页、个人中心页面,其中首页要求有文本框、表单、底部导航栏,个人中心页面有个人的信息。页面华丽,代码详细
时间: 2024-03-30 09:33:45 浏览: 95
非常感谢您的提问,以下是一个简单的购物小程序的示例代码,供您参考:
1. 登录注册页面
activity_login.xml
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="请输入密码" />
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
```
LoginActivity.java
```java
public class LoginActivity extends AppCompatActivity {
private EditText mUsernameEditText;
private EditText mPasswordEditText;
private Button mLoginButton;
private Button mRegisterButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mUsernameEditText = findViewById(R.id.et_username);
mPasswordEditText = findViewById(R.id.et_password);
mLoginButton = findViewById(R.id.btn_login);
mRegisterButton = findViewById(R.id.btn_register);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUsernameEditText.getText().toString();
String password = mPasswordEditText.getText().toString();
// TODO: 根据输入的用户名和密码进行登录操作
// 登录成功,跳转到首页
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
mRegisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到注册页面
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
```
2. 首页
activity_main.xml
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="搜索商品" />
<Spinner
android:id="@+id/sp_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/category_list"
android:prompt="@string/select_category" />
<Button
android:id="@+id/btn_filter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="筛选" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_product_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bnv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/menu_main" />
</LinearLayout>
```
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private EditText mSearchEditText;
private Spinner mCategorySpinner;
private Button mFilterButton;
private RecyclerView mProductListRecyclerView;
private BottomNavigationView mMainBottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSearchEditText = findViewById(R.id.et_search);
mCategorySpinner = findViewById(R.id.sp_category);
mFilterButton = findViewById(R.id.btn_filter);
mProductListRecyclerView = findViewById(R.id.rv_product_list);
mMainBottomNavigationView = findViewById(R.id.bnv_main);
// 设置商品列表的布局方式
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mProductListRecyclerView.setLayoutManager(layoutManager);
// 设置底部导航栏的点击事件
mMainBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_home:
// 当前已经在首页,不需要跳转
return true;
case R.id.action_cart:
// 跳转到购物车页面
Intent cartIntent = new Intent(MainActivity.this, CartActivity.class);
startActivity(cartIntent);
finish();
return true;
case R.id.action_profile:
// 跳转到个人中心页面
Intent profileIntent = new Intent(MainActivity.this, ProfileActivity.class);
startActivity(profileIntent);
finish();
return true;
}
return false;
}
});
}
}
```
3. 个人中心页面
activity_profile.xml
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_avatar"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/avatar" />
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户名" />
<TextView
android:id="@+id/tv_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="邮箱" />
<Button
android:id="@+id/btn_edit_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="编辑个人信息" />
<Button
android:id="@+id/btn_logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="退出登录" />
</LinearLayout>
```
ProfileActivity.java
```java
public class ProfileActivity extends AppCompatActivity {
private ImageView mAvatarImageView;
private TextView mNameTextView;
private TextView mEmailTextView;
private Button mEditProfileButton;
private Button mLogoutButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mAvatarImageView = findViewById(R.id.iv_avatar);
mNameTextView = findViewById(R.id.tv_name);
mEmailTextView = findViewById(R.id.tv_email);
mEditProfileButton = findViewById(R.id.btn_edit_profile);
mLogoutButton = findViewById(R.id.btn_logout);
// 设置用户的个人信息
// TODO: 从服务器获取用户的个人信息,并显示在界面上
mEditProfileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到编辑个人信息页面
Intent intent = new Intent(ProfileActivity.this, EditProfileActivity.class);
startActivity(intent);
}
});
mLogoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 清除用户的登录状态,返回到登录页面
// TODO: 清除用户的登录状态
Intent intent = new Intent(ProfileActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
```
以上是一个简单的购物小程序的示例代码,仅供参考。由于篇幅和时间的限制,代码中可能存在一些问题,需要您自行调整和完善。如果您有其他问题,欢迎随时向我提问。
阅读全文
相关推荐
















