2011-01-31 57 views
0

我试图在我的android应用程序中的edittext中记录按键次数,但即时通过一种方式来实现它。Android:按键笔画数

我认为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) { 
    } 

    }; 

每当我在主文本块中输入任何文本时,我都会收到强制关闭。我试图计算键盘输入,而不是编辑文本中的字符数量,否则它会相当直接...

非常感谢!

+2

请从logcat发布错误 – 2011-01-31 22:51:44

回答

1

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

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

1

我有同样的错误。我找到的解决方案是,你不能把一个整数到.setText()(它似乎寻找一个id)。所以你应该在keystrokeCount.setText(keyCount);必须要做的是在keyCount之前放一个“”。

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的活动字段。