2011-01-31 25 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的活動字段。