Android : EditText 文字改变的listener

editText.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable arg0) {
    // TODO Auto-generated method stub
    Integer leftLength = 30 - editText.length();

    if (leftLength >= 20) {
     textCount.setTextColor(Color.GREEN);
     textCount.setText(leftLength.toString());
    } else if (leftLength >= 5) {
     textCount.setTextColor(Color.YELLOW);
     textCount.setText(leftLength.toString());
    } else if (leftLength >= 0) {
     textCount.setText(leftLength.toString());
     textCount.setTextColor(Color.RED);

    } else {
     // editText.setEnabled(false);
     textCount.setText(String.valueOf(0));
     editText.setText(editText.getText().toString().substring(0,
       30));
    }

   }

   @Override
   public void beforeTextChanged(CharSequence arg0, int arg1,
     int arg2, int arg3) {
    // TODO Auto-generated method stub

   }

   @Override
   public void onTextChanged(CharSequence arg0, int arg1, int arg2,
     int arg3) {
    // TODO Auto-generated method stub

   }

  });

你可能感兴趣的:(listener)