Android EditText输入框实现下拉且保存最近5个历史记录思路详解

今天给大家介绍Android EditText输入框实现下拉且保存最近5个历史记录功能,android实现文本框下拉利用sharedpreferences来保存每次app启动和关闭时已经填写的数值,具体代码跟随小编一起看看吧

后面又添加了清空历史记录的标签,就是在每一次添加更新后台数组后,数组的下一个标签为清空历史记录。

1

s_btnDown.setOnClickListener(this);                           //对其进行焦点监听

1

2

3

case R.id.btnDown:

          showListPopulWindow();                          //调用显示PopuWindow 函数

      break;

点击后触发PopuWindow函数,也就是将其下拉框,绑定到TextBox标签的下面。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

private void showListPopulWindow() {

        final DeviceKeySecretManager list = ((MainActivity)getActivity()).deviceKeySecretManager;//要填充的数据

        final ListPopupWindow listPopupWindow;

        listPopupWindow = new ListPopupWindow(getActivity());

        listPopupWindow.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list.getKeyList()));//用android内置布局,或设计自己的样式

        listPopupWindow.setAnchorView(s_etAppKey);          //以哪个控件为基准,在该处以mEditText为基准

        listPopupWindow.setModal(true);

        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {          //设置项点击监听

            @Override

            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (list.KeySecretSum==i){

                    list.Clear();                                                           //点击清空

                    s_etAppKey.setText("");                            //把选择的选项内容展示在EditText上

                    s_etAppSecret.setText("");

                }else{

                    s_etAppKey.setText(list.getKeyList()[i]);                            //把选择的选项内容展示在EditText上

                    s_etAppSecret.setText(list.getSecretList()[i]);

                }

                listPopupWindow.dismiss();                             //如果已经选择了,隐藏起来

            }

        });

        listPopupWindow.show();                  //把ListPopWindow展示出来

    }

密钥管理的逻辑类:

用于在发送成功后将历史密钥信息www.qmia.cn进行缓存,后期将其绑定到下拉列表中,也为了在APP退出和首次加载时,将数据保存和提取到缓存中。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

/**

 * 标识和密钥管理

 * 最多只存储5个密钥,超过5个就开始进行循环覆盖(从第一个开始)。

 */

class DeviceKeySecretManager {

    public DeviceKeySecretManager() {

        CurrentSaveIndex = 0;

    }

    public String[] getKeyList() {

        return KeyList;

    }

    public String[] getSecretList() {

        return SecretList;

    }

    /**

     * 添加新到的key和secret到密钥库

     * 1、先判断密钥库中是否存在key,如果存在则直接更新其secret值,

     * 2、不存在则直接进行添加key/secret值。

     */

    public void addBufferKeyAndSecret(String key, String secret) {

        if (IntegerConversion.UseLoop(KeyList,key)) {

            int index=0;

            for (int i=0;i<KeyList.length;i++) {

                if (KeyList[i].equals(key)){

                    index=i;

                    break;

                }

            }

            KeyList[index]=key;

            SecretList[index]=secret;

        } else {

            if (KeySecretSum == 5) {

                CurrentSaveIndex = CurrentSaveIndex == 5 ? 0 : CurrentSaveIndex;

                KeyList[CurrentSaveIndex] = key;

                SecretList[CurrentSaveIndex] = secret;

                CurrentSaveIndex++;

            } else {

                KeyList[CurrentSaveIndex] = key;

                SecretList[CurrentSaveIndex] = secret;

                CurrentSaveIndex++;

                KeySecretSum++;

                KeyList[CurrentSaveIndex] = "清空历史记录";

            }

        }

    }

    public void Clear() {

        CurrentSaveIndex = 0;

        KeySecretSum = 0;

        for (int i = 0; i < KeyList.length; i++) {

            KeyList[i] = null;

        }

        for (int i = 0; i < SecretList.length; i++) {

            SecretList[i] = null;

        }

    }

    public int CurrentSaveIndex = 0;                    //当前保存的序号

    public int KeySecretSum = 0;                        //key的总个数,最多存储5个。

    private String[] KeyList = new String[6];

    private String[] SecretList = new String[5];

}

APP退出和首次加载时,对数据在本地进行保存和提取;

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

/**

     * 读取保存的文件

     */

    private void getSavedPreference() {

        try {

            SharedPreferences pref = this.getSharedPreferences(getResources().getString(R.string.app_name), MODE_PRIVATE);

            int sum=pref.getInt("KeySecretSum", 0);

            for (int i=0;i<=sum;i++){

                deviceKeySecretManager.getKeyList()[i]=pref.getString("Key"+i, "");

            }

            for (int i=0;i<sum;i++){

                deviceKeySecretManager.getSecretList()[i]=pref.getString("Secret"+i, "");

            }

            deviceKeySecretManager.CurrentSaveIndex=sum==5?0:sum;

            deviceKeySecretManager.KeySecretSum=sum;

        } catch (Exception ex) {

        }

    }

    /**

     * 保存文件

     * */

    private void setSavePreference() {

        try {

            SharedPreferences pref = getSharedPreferences(getResources().getString(R.string.app_name), MODE_PRIVATE);

            SharedPreferences.Editor edit = pref.edit();

            edit.putInt("KeySecretSum", deviceKeySecretManager.KeySecretSum);             //现有保存的总个数

            for (int i=0;i<=deviceKeySecretManager.KeySecretSum;i++){

                edit.putString("Key"+i, deviceKeySecretManager.getKeyList()[i]);

            }

            for (int i=0;i<deviceKeySecretManager.KeySecretSum;i++){

                edit.putString("Secret"+i, deviceKeySecretManager.getSecretList()[i]);

            }

            edit.commit();

        } catch (Exception ex) {

        }

    }

下面是当发送成功后的业务逻辑:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

@Override

    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.btnSendData:

                if (!DeviceManager.getInstance().DeviceIsConnected()) {

                    tu.ToastShow(context, "设备已断开连接,无法进行通讯。");

                    return;

                }

                if (DeviceManager.getInstance().DeviceIsBusy()) {

                    tu.ToastShow(context, "设备忙碌,请等待...");

                    return;

                }

                try {

                    String key,secret;

                    key=s_etAppKey.getText().toString();

                    secret=s_etAppSecret.getText().toString();

                    if (key.length()<=0||secret.length()<=0||

                            TextUtils.isEmpty(key)||TextUtils.isEmpty(secret)){

                        tu.ToastShow(context, "标识和密钥不能为空!");

                        return;

                    }

                    //调用方法拼接字符串,发送给下位机设备。

                    int nResult = DeviceManager.getInstance().WriteRTKData(context, new byte[]{});

                    if (nResult > 0) {

                        tu.ToastShow(context, "参数写入成功");

                        ((MainActivity)getActivity()).deviceKeySecretManager.addBufferKeyAndSecret(key,secret);

                    }

                } catch (Exception ex) {

                    tu.ToastShow(context, "参数写入失败!");

                }

                break;

            case R.id.btnClearData:                                     //只清空当前的标识和密钥

                s_etAppKey.setText("");

                s_etAppSecret.setText("");

                break;

            case R.id.btnDown:

                showListPopulWindow();                          //调用显示PopuWindow 函数

                break;

            default:

                break;

        }

    }

总结:

通过上面的业务分析,代码实现就可以实现具体的需求,保存下最近5个的历史记录。

其实对于写程序而言,难的不是语法和技巧,而是编程思想,对于同一个问题/需求,不同的人有不同的解决办法,谁也不能说谁的方法是错误的,只能说谁的方法是目前为止最有效的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,下拉框和输入框都是常用的UI组件。下面分别介绍它们的实现方法: 1. 下拉框(Spinner) 下拉框是用来供用户选择一个或多个选项的控件,通常用于表单中的选项选择。 在XML布局文件中,可以用Spinner标签来定义一个下拉框: ``` <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 接着,在Java代码中需要为Spinner设置一个Adapter,来显示下拉框中的选项。下面是示例代码: ``` Spinner spinner = findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.options, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); ``` 其中,R.array.options是存储选项文本的字符串数组,可以在res/values/strings.xml文件中定义,如下: ``` <string-array name="options"> <item>选项1</item> <item>选项2</item> <item>选项3</item> </string-array> ``` 2. 输入框EditText输入框是用来供用户输入文本内容的控件,通常用于表单中的文本输入。 在XML布局文件中,可以用EditText标签来定义一个输入框: ``` <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入文本" /> ``` 接着,在Java代码中可以通过findViewById()方法获取输入框对象,并对其进行操作,如下: ``` EditText editText = findViewById(R.id.edit_text); String text = editText.getText().toString(); // 获取输入框中的文本内容 editText.setText("Hello world!"); // 设置输入框的文本内容 ``` 除此之外,还可以为EditText设置输入类型、最大长度、输入提示等属性,具体可查看Android官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值