按顺序添加数字(TextView)

时间:2015-04-14 01:30:49

标签: java android

我正在尝试按顺序添加数字。我的意思是让它们彼此相邻。例如,我想要一个数字键盘,当用户输入数字时,它会将它添加到显示字段等。

例如(显示):1738 用户点击了键1,7,3,8。

从那里我将把这个值加到textView的double或string中。当人们点击按钮时,我只想让每个号码彼此相邻。

非常感谢。

1 个答案:

答案 0 :(得分:0)

我认为您已经知道如何在TextView中显示值。

作为数据结构,您可以选择两件事。

  1. 的ArrayList
  2. LinkedHashMap的
  3. 我会使用LinkedHashMap。它具有唯一性,序列维持性,并且可以有额外的Value

    要插入,只需使用put方法,如下所示。由于HashMap只保留一个密钥,它会自动检测重复项并覆盖到新密钥。

    LinkedHashMap<String, Date> hashMap = new LinkedHashMap<>();
    hashMap.put("1", new Date()); // last updated timestamp is placed as a value.
    

    要按索引获取插入的String值,请使用以下方法。

    public String indexOf(int position){
        return (String) hashMap.keySet().toArray()[position];
    }
    

    要在TextView中显示,请在每个键中使用以下方法单击或触摸。

    String output;
    for(int i=0; i < hashMap.size(); i++)
    {
         output += hashMap.keySet().toArray()[i];
         if(i < hashMap.size()-1)
             output += ", ";
    }
    textViewOutput.setText(output);