开发者

Android: Key Stroke Count

Im trying to record the number of keystrokes in an edittext in my android app, but im coming up blank with a way to do it.

Im thinking that a TextWatcher is the way to achieve this, but nothing is working at the moment. Im a novice android developer, so any help much appreciated.

 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) {
  }

    };

Im getting a force close every time I enter any text into the main text block. Im trying to count the keystrokes, not the number of characters within the edittext, otherwise it would be fairly straight forward...

Many thanks!


ERROR/AndroidRuntime(482): android.content.res.Resources$NotFoundException: String resource ID #0x1

This means that you are trying to reference a resource that doesn't exist. I don't see anything in the code snippet that looks like it would be the problem, but right after that logcat snippet should be another line that includes a file and line number. This should point you to the exact problem.


I had the same error. The solution I found was that you can't put an integer into the .setText() (it seems it looks for a id). So all you should have to do in keystrokeCount.setText(keyCount); is put a "" before keyCount.


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) {
        }
    });

where keycounter is a field of the activity that contains the EditText.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜