Android实现可复用的筛选页面

这篇文章主要为大家详细介绍了Android实现可复用的筛选页面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现可复用的筛选页面的具体代码,供大家参考,具体内容如下

窗口代码


/**
 * 筛选页面
 * 1.将用户的输入转换成sql语句
 * 2.涉及到精确查询和模糊查询
 * 3.提交数据之后需要刷新列表
 */
public class UserFilterActivity extends AppCompatActivity implements View.OnClickListener {

 private List<ImageView> imageViewList;
 private Context mContext;

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_user_filter);
  ButterKnife.bind(this);

  initView();
  initData();
  initListener();
 }

 private void initData() {

  mContext = this;
  title.setText("筛选");

  //将所有的ImageView实例存进list
  imageViewList = new ArrayList<>();
  imageViewList.add(ivNumber);
  imageViewList.add(ivMobile);
  imageViewList.add(ivWorkNumber);
  imageViewList.add(ivSysName);
  imageViewList.add(ivPost);
  imageViewList.add(ivTelephone);
  imageViewList.add(ivEmail);

  //初始化ImageView,把状态放进tag里面
  // 所有的ImageView都是未选中状态
  for (ImageView imageView : imageViewList) {
   imageView.setTag(false);
  }
 }

 private void initView() {
  editTv.setVisibility(View.VISIBLE);
  editTv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_send_black_24dp, 0, 0, 0);
 }

 private void initListener() {

  //给ImageView设置监听
  for (ImageView imageView : imageViewList) {
   imageView.setOnClickListener(this);
  }

  //单选按钮设置监听
  rgStatus.setOnCheckedChangeListener((group, checkedId) -> {
   if (checkedId == R.id.rb_status_off) {
    offTime.setVisibility(View.VISIBLE);
    line.setVisibility(View.VISIBLE);
   } else {
    offTime.setVisibility(View.GONE);
    line.setVisibility(View.GONE);
   }
  });

  titleLeft.setOnClickListener(this);
  editTv.setOnClickListener(this);
  tvUserName.setOnClickListener(this);
  tvCreator.setOnClickListener(this);
 }

 @Override
 public void onClick(View view) {

  if (view.getId() == R.id.title_left) {
   finish();
  }

  if (view.getId() == R.id.edit_tv) {
   submit();
  }

  //选择用户,这里是多选
  if (view.getId() == R.id.tv_user_name) {
   Intent intent = new Intent(mContext, UserSelectActivity.class);
   intent.putExtra("isMultipleEnable", true);
   startActivityForResult(intent, 15476);
  }

  //选择用户,这里是单选
  if (view.getId() == R.id.tv_creator) {
   Intent intent = new Intent(mContext, UserSelectActivity.class);
   startActivityForResult(intent, 13685);
  }

  //点击checkbox的动作
  switch (view.getId()) {
   case R.id.iv_number:
   case R.id.iv_work_number:
   case R.id.iv_sys_name:
   case R.id.iv_post:
   case R.id.iv_mobile:
   case R.id.iv_telephone:
   case R.id.iv_email:
    switchExact((ImageView) view);
  }
 }

 //拼接sql语句,并回传给上一个页面然后结束此页
 //字段仅供参考
 public void submit() {
  StringBuilder sb = new StringBuilder("where");
  String number = etNumber.getText().toString();
  String workNumber = etWorkNumber.getText().toString();
  String sysName = etSysName.getText().toString();
  String post = etPost.getText().toString();
  String mobile = etMobile.getText().toString();
  String telePhone = etTelePhone.getText().toString();
  String email = etEmail.getText().toString();

  if (!TextUtils.isEmpty(number)) {
   String[] arr = number.split(",");
   if ((boolean) ivNumber.getTag()) {
    sb.append(" and userID in (").append(stringToString(arr)).append(")");
   } else {
    for (int i = 0; i < arr.length; i++) {
     if (i == 0) {
      sb.append(" and (left(userId, ").append(arr[i].length()).append(") = '").append(arr[i]).append("'");
     } else {
      sb.append(" or left(userId, ").append(arr[i].length()).append(") = '").append(arr[i]).append("'");
     }
    }
    sb.append(")");
   }
  }

  if (!TextUtils.isEmpty(sysName)) {
   String[] arr = sysName.split(",");
   if ((boolean) ivSysName.getTag()) {
    sb.append(" and userName in (").append(stringToString(arr)).append(")");
   } else {
    for (int i = 0; i < arr.length; i++) {
     if (i == 0) {
      sb.append(" and (userName like '|").append(arr[i]).append("|'");
     } else {
      sb.append(" or userName like '|").append(arr[i]).append("|'");
     }
    }
    sb.append(")");
   }
  }

  if (!TextUtils.isEmpty(workNumber)) {
   String[] arr = workNumber.split(",");
   if ((boolean) ivWorkNumber.getTag()) {
    sb.append(" and workNumber in (").append(stringToString(arr)).append(")");
   } else {
    for (int i = 0; i < arr.length; i++) {
     if (i == 0) {
      sb.append(" and (workNumber like '").append(arr[0]).append("|'");
     } else {
      sb.append(" or workNumber like '").append(arr[i]).append("|'");
     }
    }
    sb.append(")");
   }
  }

  if (!TextUtils.isEmpty(post)) {
   String[] arr = post.split(",");
   if ((boolean) ivPost.getTag()) {
    sb.append(" and workPos in (").append(stringToString(arr)).append(")");
   } else {
    for (int i = 0; i < arr.length; i++) {
     if (i == 0) {
      sb.append(" and (workPost like '|").append(arr[0]).append("|'");
     } else {
      sb.append(" or workPost like '|").append(arr[i]).append("|'");
     }
    }
    sb.append(")");
   }
  }

  if (!TextUtils.isEmpty(mobile)) {
   String[] arr = mobile.split(",");
   if ((boolean) ivMobile.getTag()) {
    sb.append(" and mobile in (").append(stringToString(arr)).append(")");
   } else {
    for (int i = 0; i < arr.length; i++) {
     sb.append(" and mobileNumber in ('").append(stringToString(arr)).append("')");
     if (i == 0) {
      sb.append(" and (left(mobileNumber, ").append(arr[i].length()).append(") = '").append(arr[i]).append("'");
     } else {
      sb.append(" or left(mobileNumber, ").append(arr[i].length()).append(") = '").append(arr[i]).append("'");
     }
    }
    sb.append(")");
   }
  }

  if (!TextUtils.isEmpty(telePhone)) {
   String[] arr = telePhone.split(",");
   if ((boolean) ivTelephone.getTag()) {
    sb.append(" and officeNumber in (").append(stringToString(arr)).append(")");
   } else {
    for (int i = 0; i < arr.length; i++) {
     if (i == 0) {
      sb.append(" and (left(officeNumber, ").append(arr[i].length()).append(") = '").append(arr[i]).append("'");
     } else {
      sb.append(" or left(officeNumber, ").append(arr[i].length()).append(") = '").append(arr[i]).append("'");
     }
    }
    sb.append(")");
   }
  }

  if (!TextUtils.isEmpty(email)) {
   String[] arr = email.split(",");
   if ((boolean) ivEmail.getTag()) {
    sb.append(" and email in (").append(stringToString(arr)).append(")");
   } else {
    for (int i = 0; i < arr.length; i++) {
     if (i == 0) {
      sb.append(" and (email LIKE '|").append(arr[i]).append("|'");
     } else {
      sb.append(" or email LIKE '|").append(arr[i]).append("|'");
     }
    }
    sb.append(")");
   }
  }

  Intent intent = new Intent();
  String sql = sb.toString().replaceFirst(" and", "");

  intent.putExtra("sql", sql);
  setResult(123, intent);
  finish();
 }

 //切换checkbox状态
 public void switchExact(ImageView view) {
  view.setTag(!(boolean) view.getTag());
  if ((boolean) view.getTag()) {
   view.setImageResource(R.drawable.ic_check_box_black_24dp);
  } else {
   view.setImageResource(R.drawable.ic_check_box_outline_blank_black_24dp);
  }
 }

 private String stringToString(String[] array) {
  StringBuilder end = new StringBuilder("");
  for (String s : array) {
   end.append("'").append(s).append("',");
  }
  return end.toString().substring(0, end.length() - 1);
 }

 @Override
 //回传值的处理
 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

  if (requestCode == 15476 && resultCode == RESULT_OK) {
   ArrayList<ClsNormalUser> users = data.getParcelableArrayListExtra("users");
   StringBuilder sb = new StringBuilder();

   for (ClsNormalUser user : users) {
    sb.append(user.getCName()).append(",");
   }
   tvUserName.setText(sb.toString());
  }

  if (requestCode == 13685 && resultCode == RESULT_OK) {
   ClsNormalUser user = data.getParcelableExtra("user");
   tvCreator.setText(user.getCName());
  }

  super.onActivityResult(requestCode, resultCode, data);
 }
}

效果如图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

sqlite是嵌入式的和轻量级的sql数据库。sqlite是由c实现的。广泛用于包括浏览器(支持html5的大部分浏览器,ie除外)、ios、android以及一些便携需求的小型web应用系统
这篇文章主要为大家详细介绍了android自定义滚轴选择器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了Android实现随意拖动View效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了Android Native 内存泄漏系统化解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要为大家详细介绍了Android实现屏幕手写签名,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了Android 折叠屏适配攻略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧