相关视频教程在某站上面(🔍浩宇软件开发)
1. 项目功能思维导图
2. 项目涉及到的技术点
- 使用SQLite数据库实现数据存储
- 使用CountDownTimer实现启动页倒计时
- 使用SharedPreferences实现记住密码登录
- 使用BottomNavigationView实现底部导航栏
- 使用Activity+Fragment实现底部导航栏页面之间切换
- 使用RecyclerView+adapter实现商品列表
- 使用CardView卡片控件实现头像圆角
- 使用AlertDialog自定义布局来实现收货地址
3. 开发环境
- 开发工具:Androidstudio
- 开发语言:Java
- jdk版本:11+以上
4. 项目截图
5. 项目部分功能代码实现
- 底部导航栏实现
public class MainActivity extends AppCompatActivity {
private HomeFragment mHomeFragment;
private CarFragment mCarFragment;
private OrderFragment mOrderFragment;
private MineFragment mMineFragment;
private BottomNavigationView mBottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
mBottomNavigationView = findViewById(R.id.bottomNavigationView);
//mBottomNavigationView设置点击事件
mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == R.id.home) {
selectedFragment(0);
} else if (item.getItemId() == R.id.car) {
selectedFragment(1);
} else if (item.getItemId() == R.id.order) {
selectedFragment(2);
} else {
selectedFragment(3);
}
return true;
}
});
//默认首页选中
selectedFragment(0);
}
private void selectedFragment(int position) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
hideFragment(fragmentTransaction);
if (position == 0) {
if (mHomeFragment == null) {
mHomeFragment = new HomeFragment();
fragmentTransaction.add(R.id.content, mHomeFragment);
} else {
fragmentTransaction.show(mHomeFragment);
}
} else if (position == 1) {
if (mCarFragment == null) {
mCarFragment = new CarFragment();
fragmentTransaction.add(R.id.content, mCarFragment);
} else {
fragmentTransaction.show(mCarFragment);
mCarFragment.loadData();
}
} else if (position == 2) {
if (mOrderFragment == null) {
mOrderFragment = new OrderFragment();
fragmentTransaction.add(R.id.content, mOrderFragment);
} else {
fragmentTransaction.show(mOrderFragment);
mOrderFragment.loadData();
}
} else {
if (mMineFragment == null) {
mMineFragment = new MineFragment();
fragmentTransaction.add(R.id.content, mMineFragment);
} else {
fragmentTransaction.show(mMineFragment);
}
}
//一定要提交
fragmentTransaction.commit();
}
private void hideFragment(FragmentTransaction fragmentTransaction) {
if (mHomeFragment != null) {
fragmentTransaction.hide(mHomeFragment);
}
if (mCarFragment != null) {
fragmentTransaction.hide(mCarFragment);
}
if (mOrderFragment != null) {
fragmentTransaction.hide(mOrderFragment);
}
if (mMineFragment != null) {
fragmentTransaction.hide(mMineFragment);
}
}
}
- 修改密码
public class UpdatePwdActivity extends AppCompatActivity {
private EditText et_new_password;
private EditText et_confirm_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_pwd);
//初始化控件
et_new_password =findViewById(R.id.et_new_password);
et_confirm_password =findViewById(R.id.et_confirm_password);
//修改密码点击事件
findViewById(R.id.btn_update_pwd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String new_pwd =et_new_password.getText().toString();
String confirm_pwd =et_confirm_password.getText().toString();
if (TextUtils.isEmpty(new_pwd) || TextUtils.isEmpty(confirm_pwd)){
Toast.makeText(UpdatePwdActivity.this, "信息不能为空", Toast.LENGTH_SHORT).show();
}else if(!new_pwd.equals(confirm_pwd)){
Toast.makeText(UpdatePwdActivity.this, "新密码和确认密码不一致", Toast.LENGTH_SHORT).show();
}else {
UserInfo userInfo = UserInfo.getUserInfo();
if (null!=userInfo){
int row = UserDbHelper.getInstance(UpdatePwdActivity.this).updatePwd(userInfo.getUsername(), new_pwd);
if (row>0){
Toast.makeText(UpdatePwdActivity.this, "密码修改成功,请重新登录", Toast.LENGTH_SHORT).show();
//回传的时候要用 startActivityForResult启动一个页面,并且在该页面要设置setResult
setResult(1000);
finish();
}else {
Toast.makeText(UpdatePwdActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
}
}
}
}
});
//返回
findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
- 商品详情
public class ProductDetailsActivity extends AppCompatActivity {
private ImageView product_img;
private TextView product_title;
private TextView product_price;
private TextView product_details;
private ProductInfo productInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
//获取传递的数据
productInfo = (ProductInfo) getIntent().getSerializableExtra("productInfo");
//返回
findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
//初始化控件
product_img = findViewById(R.id.product_img);
product_title = findViewById(R.id.product_title);
product_price = findViewById(R.id.product_price);
product_details = findViewById(R.id.product_details);
//设置数据
if (null != productInfo) {
product_img.setImageResource(productInfo.getProduct_img());
product_title.setText(productInfo.getProduct_title());
product_details.setText(productInfo.getProduct_details());
product_price.setText(productInfo.getProduct_price() + "");
}
//加入购物车
findViewById(R.id.addCar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(ProductDetailsActivity.this)
.setTitle("确认是否加入到购物车?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
UserInfo userInfo = UserInfo.getUserInfo();
if (userInfo != null) {
//加入到购物车
int row = CarDbHelper.getInstance(ProductDetailsActivity.this).addCar(userInfo.getUsername(), productInfo.getProduct_id(), productInfo.getProduct_img(), productInfo.getProduct_title(), productInfo.getProduct_price());
if (row > 0) {
Toast.makeText(ProductDetailsActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(ProductDetailsActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
}
}
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
});
}
}