您好,登錄后才能下訂單哦!
這篇文章主要介紹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如何實現可復用的篩選頁面”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。