Android EditText輸入框實現下拉且保存最近5個歷史記錄
文章結構:
一、需求闡述
技術部同事提出想要在APP上保存最近輸入成功的5個密鑰信息,同時支持可以下拉進行選擇。
這也是為了方便客戶在現在多次輸入信息,幫助其快速進行輸入。
二、實現思路:
目前想要實現的需求
1、想要實現保存用戶輸入的密鑰信息。
2、通過點擊右側的下拉來觸發,讓用戶去選擇已經發送成功的信息。
3、通過SharedPreferences來保存每次APP退出後的數據。
4、當發送成功後,更新後台的存儲數據,進行邏輯判斷。
三、代碼邏輯
下面圖片是最終的實現效果,當輸入標識和密鑰,點擊發送按鈕,成功後將數據自動保存到後台的數組中。點擊右側的下拉圖標後,在將其彈出。
後面又添加了清空歷史記錄的標籤,就是在每一次添加更新後台數組後,數組的下一個標籤為清空歷史記錄。
1 s_btnDown.setOnClickListener(this); //對其進行焦點監聽
1 case R.id.btnDown:
2 showListPopulWindow(); //調用顯示PopuWindow 函數
3 break;
點擊後觸發PopuWindow函數,也就是將其下拉框,綁定到TextBox標籤的下面。
1 private void showListPopulWindow() {
2 final DeviceKeySecretManager list = ((MainActivity)getActivity()).deviceKeySecretManager;//要填充的數據
3 final ListPopupWindow listPopupWindow;
4 listPopupWindow = new ListPopupWindow(getActivity());
5 listPopupWindow.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list.getKeyList()));//用android內置布局,或設計自己的樣式
6 listPopupWindow.setAnchorView(s_etAppKey); //以哪個控件為基準,在該處以mEditText為基準
7 listPopupWindow.setModal(true);
8
9 listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { //設置項點擊監聽
10 @Override
11 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
12 if (list.KeySecretSum==i){
13 list.Clear(); //點擊清空
14 s_etAppKey.setText(""); //把選擇的選項內容展示在EditText上
15 s_etAppSecret.setText("");
16 }else{
17 s_etAppKey.setText(list.getKeyList()[i]); //把選擇的選項內容展示在EditText上
18 s_etAppSecret.setText(list.getSecretList()[i]);
19 }
20
21 listPopupWindow.dismiss(); //如果已經選擇了,隱藏起來
22 }
23 });
24 listPopupWindow.show(); //把ListPopWindow展示出來
25 }
密鑰管理的邏輯類:
用於在發送成功後將歷史密鑰信息進行緩存,後期將其綁定到下拉列表中,也為了在APP退出和首次加載時,將數據保存和提取到緩存中。
1 /**
2 * 標識和密鑰管理
3 * 最多只存儲5個密鑰,超過5個就開始進行循環覆蓋(從第一個開始)。
4 */
5 class DeviceKeySecretManager {
6
7 public DeviceKeySecretManager() {
8 CurrentSaveIndex = 0;
9 }
10
11 public String[] getKeyList() {
12 return KeyList;
13 }
14
15 public String[] getSecretList() {
16 return SecretList;
17 }
18
19 /**
20 * 添加新到的key和secret到密鑰庫
21 * 1、先判斷密鑰庫中是否存在key,如果存在則直接更新其secret值,
22 * 2、不存在則直接進行添加key/secret值。
23 */
24 public void addBufferKeyAndSecret(String key, String secret) {
25 if (IntegerConversion.UseLoop(KeyList,key)) {
26 int index=0;
27 for (int i=0;i<KeyList.length;i++) {
28 if (KeyList[i].equals(key)){
29 index=i;
30 break;
31 }
32 }
33 KeyList[index]=key;
34 SecretList[index]=secret;
35 } else {
36 if (KeySecretSum == 5) {
37 CurrentSaveIndex = CurrentSaveIndex == 5 ? 0 : CurrentSaveIndex;
38 KeyList[CurrentSaveIndex] = key;
39 SecretList[CurrentSaveIndex] = secret;
40 CurrentSaveIndex++;
41 } else {
42 KeyList[CurrentSaveIndex] = key;
43 SecretList[CurrentSaveIndex] = secret;
44 CurrentSaveIndex++;
45 KeySecretSum++;
46 KeyList[CurrentSaveIndex] = "清空歷史記錄";
47 }
48 }
49 }
50
51 public void Clear() {
52 CurrentSaveIndex = 0;
53 KeySecretSum = 0;
54
55 for (int i = 0; i < KeyList.length; i++) {
56 KeyList[i] = null;
57 }
58
59 for (int i = 0; i < SecretList.length; i++) {
60 SecretList[i] = null;
61 }
62 }
63
64 public int CurrentSaveIndex = 0; //當前保存的序號
65 public int KeySecretSum = 0; //key的總個數,最多存儲5個。
66 private String[] KeyList = new String[6];
67 private String[] SecretList = new String[5];
68 }
APP退出和首次加載時,對數據在本地進行保存和提取;
1 /**
2 * 讀取保存的文件
3 */
4 private void getSavedPreference() {
5 try {
6 SharedPreferences pref = this.getSharedPreferences(getResources().getString(R.string.app_name), MODE_PRIVATE);
7 int sum=pref.getInt("KeySecretSum", 0);
8
9 for (int i=0;i<=sum;i++){
10 deviceKeySecretManager.getKeyList()[i]=pref.getString("Key"+i, "");
11 }
12
13 for (int i=0;i<sum;i++){
14 deviceKeySecretManager.getSecretList()[i]=pref.getString("Secret"+i, "");
15 }
16
17 deviceKeySecretManager.CurrentSaveIndex=sum==5?0:sum;
18 deviceKeySecretManager.KeySecretSum=sum;
19 } catch (Exception ex) {
20
21 }
22 }
23
24 /**
25 * 保存文件
26 * */
27 private void setSavePreference() {
28 try {
29 SharedPreferences pref = getSharedPreferences(getResources().getString(R.string.app_name), MODE_PRIVATE);
30 SharedPreferences.Editor edit = pref.edit();
31 edit.putInt("KeySecretSum", deviceKeySecretManager.KeySecretSum); //現有保存的總個數
32
33 for (int i=0;i<=deviceKeySecretManager.KeySecretSum;i++){
34 edit.putString("Key"+i, deviceKeySecretManager.getKeyList()[i]);
35 }
36
37 for (int i=0;i<deviceKeySecretManager.KeySecretSum;i++){
38 edit.putString("Secret"+i, deviceKeySecretManager.getSecretList()[i]);
39 }
40 edit.commit();
41 } catch (Exception ex) {
42
43 }
44 }
下面是當發送成功後的業務邏輯:
1 @Override
2 public void onClick(View v) {
3 switch (v.getId()) {
4 case R.id.btnSendData:
5 if (!DeviceManager.getInstance().DeviceIsConnected()) {
6 tu.ToastShow(context, "設備已斷開連接,無法進行通訊。");
7 return;
8 }
9 if (DeviceManager.getInstance().DeviceIsBusy()) {
10 tu.ToastShow(context, "設備忙碌,請等待...");
11 return;
12 }
13 try {
14 String key,secret;
15 key=s_etAppKey.getText().toString();
16 secret=s_etAppSecret.getText().toString();
17
18 if (key.length()<=0||secret.length()<=0||
19 TextUtils.isEmpty(key)||TextUtils.isEmpty(secret)){
20 tu.ToastShow(context, "標識和密鑰不能為空!");
21 return;
22 }
23
24 //調用方法拼接字符串,發送給下位機設備。
25 int nResult = DeviceManager.getInstance().WriteRTKData(context, new byte[]{});
26 if (nResult > 0) {
27 tu.ToastShow(context, "參數寫入成功");
28
29 ((MainActivity)getActivity()).deviceKeySecretManager.addBufferKeyAndSecret(key,secret);
30 }
31 } catch (Exception ex) {
32 tu.ToastShow(context, "參數寫入失敗!");
33 }
34 break;
35 case R.id.btnClearData: //只清空當前的標識和密鑰
36 s_etAppKey.setText("");
37 s_etAppSecret.setText("");
38 break;
39 case R.id.btnDown:
40 showListPopulWindow(); //調用顯示PopuWindow 函數
41 break;
42 default:
43 break;
44 }
45 }
總結:
通過上面的業務分析,代碼實現就可以實現具體的需求,保存下最近5個的歷史記錄。
其實對於寫程序而言,難的不是語法和技巧,而是編程思想,對於同一個問題/需求,不同的人有不同的解決辦法,誰也不能說誰的方法是錯誤的,只能說誰的方法是目前為止最有效的。
小寄語
一個人的奮鬥,像懷孕一樣,日子久了,總會被看出來的。
人生短暫,我不想去追求自己看不見的,我只想抓住我能看的見的。
我是哉說,謝謝您的閱讀,希望和你一起進步、成長。
如果對您有幫助,麻煩點贊,轉發。