欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现可复用的筛选页面

程序员文章站 2023-02-21 21:42:04
本文实例为大家分享了android实现可复用的筛选页面的具体代码,供大家参考,具体内容如下 窗口代码 /** * 筛选页面 * 1.将用户的输入转换成s...

本文实例为大家分享了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);
 }
}

效果如图

Android实现可复用的筛选页面

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