我想在添加一封信时计算一下?

时间:2015-12-12 08:32:33

标签: android

TextWatcher myTextWathcer = new TextWatcher() {

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after){
        TVinput.setText("Not Enetered");
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int count, int after){
        Toast.makeText(Display.this, "YOU PRESSED ", Toast.LENGTH_SHORT).show();
        Log.d("HEY", "You pressed ");
        countForEachLetter++;
    }

    @Override
    public void afterTextChanged(Editable s){
        if(s.length()==0)
            TVinput.setText("Not Enetered");
        else if (s.length()>0 && s.length()<6)
            TVinput.setText("Please complete fully");
    }
    settings.addTextChangedListener(myTextWathcer);
}

我正在使用文本观察器检查editText的更改,我想为每个添加或删除的字母显示吐司,并且还得到一个计数,我使用countForEachLetter(一个浮点值),但我总是得到零作为答案?

2 个答案:

答案 0 :(得分:0)

试试这个。

private TextView mTextView;
private EditText mEditText;
private final TextWatcher mTextEditorWatcher = new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
           //This sets a textview to the current length
           mTextView.setText(String.valueOf(s.length()));
        }

        public void afterTextChanged(Editable s) {
        }
};

并将textwatcher设置如下

mEditText.addTextChangedListener(mTextEditorWatcher);

答案 1 :(得分:0)

我发现问题在下一个类我传递的值我在使用传递的意图中使用了不同的字符串值,并且这个方法工作正常:D