您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。