在 android 中从 textwatcher 获取 edittext.settext 数据

getting edittext.settext data from textwatcher in android

我正在 EditText 中设置文本,但是当从 TextWatcher 中获取文本时,它给出的是空字符串。

请检查此代码

for (int i = 0; i < 4; i++) {
   et = new EditText(this);
   et.setText("hai");
   final EditText finalEt = et;
   ans.addView(et);
   final int finalI = i;
   finalEt.addTextChangedListener(new TextWatcher() {
       public void afterTextChanged(Editable s) {
           Log.d("text is",""+finalEt.getText().toString());
       }

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

       public void onTextChanged(CharSequence s, int start,int before, int count) {}
   }

你会在 onTextChangedafterTextChanged 上得到它,就像这样。

for (int i = 0; i < 4; i++) {
et = new EditText(this);
et.setText("hai");

final EditText finalEt = et;

ans.addView(et);
final int finalI = i;
finalEt.addTextChangedListener(new TextWatcher() {
   public void afterTextChanged(Editable s) {
                   String str = s.toString();
           Log.d("text is",""+s);

   }

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

   public void onTextChanged(CharSequence s, int start,int before, int count) {
           Log.d("text is",""+s.toString());
   }
}

用这个替换日志

Log.d("text is","" + new String(s.toString()));