Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:用户键入EditText时创建倒计时字字段_Android - Fatal编程技术网

Android:用户键入EditText时创建倒计时字字段

Android:用户键入EditText时创建倒计时字字段,android,Android,我想在TextView中显示一个计数器,它随着用户在EditText中写入的字符数而减少 我有一个TextView来显示110个字符的计数器,109、108、107 wordCount = (TextView) findViewById(R.id.nouvelle_annonce_words_count) ; 我有一个EditText,用户在其中写道: title = (EditText) findViewById(R.id.nouvelle_annonce_titre) ; 我如何监听Ed

我想在
TextView
中显示一个计数器,它随着用户在EditText中写入的字符数而减少

我有一个
TextView
来显示110个字符的计数器,109、108、107

wordCount = (TextView) findViewById(R.id.nouvelle_annonce_words_count) ;
我有一个EditText,用户在其中写道:

title = (EditText) findViewById(R.id.nouvelle_annonce_titre) ;

我如何监听EditText中的更改并用剩余字符数更新我的textView?

您可以像这样使用
addTextChangedListener

title.addTextChangedListener(new TextWatcher() {

  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
      wordCount.setText(String.valueOf(110 - (title.getText().toString().length)));
  }

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

  }

  @Override
  public void afterTextChanged(Editable s) {

  }
});

你想做的事叫做

可能重复的
title.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            wordCount.setText(String.valueOf(110-s.length()));
        }

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

            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {

            // TODO Auto-generated method stub
        }
    });