0

我试图在我的 android 应用程序中的 edittext 中记录击键次数,但我想不出办法来做到这一点。

我认为 TextWatcher 是实现这一目标的方法,但目前没有任何效果。我是一名新手 android 开发人员,因此非常感谢任何帮助。

 mainTextBlock.addTextChangedListener(keyCounter);
    ...
    TextWatcher keyCounter = new TextWatcher() {

  public void afterTextChanged(Editable s) { 
   keyCount++;
   TextView keystrokeCount = (TextView)findViewById(R.id.keystrokeCount);
   keystrokeCount.setText(keyCount);
  }

  public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {   
  }

  public void onTextChanged(CharSequence s, int start, int before,
    int count) {
  }

    };

每次在主文本块中输入任何文本时,我都会感到力不从心。我试图计算击键,而不是编辑文本中的字符数,否则它会相当简单......

非常感谢!

4

3 回答 3

1

我有同样的错误。我找到的解决方案是您不能将整数放入.setText()(它似乎在寻找一个 id)。所以你所要做的keystrokeCount.setText(keyCount);就是在前面加上一个“” keyCount

于 2011-11-22T17:36:03.057 回答
1
yourEditText.addTextChangedListener(new TextWatcher() { //per contare i tasti       
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            keycounter++;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }       
        @Override
        public void afterTextChanged(Editable s) {
        }
    });

其中 keycounter 是包含 EditText 的活动字段。

于 2013-02-12T11:30:20.973 回答
1

错误/AndroidRuntime(482): android.content.res.Resources$NotFoundException: 字符串资源 ID #0x1

这意味着您正在尝试引用不存在的资源。我在代码片段中看不到任何看起来可能是问题的东西,但是在该 logcat 片段之后应该是另一行,其中包含文件和行号。这应该指出您确切的问题。

于 2011-02-01T00:34:47.697 回答