2016-04-14 62 views
0

我有兩個編輯文本,每一個有文字守望者,當第一的EditText文本改變時改變第二的EditText文本,似乎代碼創建一個循環,是有什麼我可以做,使文本守望者只聽用戶輸入,而不是通過代碼改變文本?的EditText addTextChangedListener環

et1.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

      double x = Double.valueOf(et1.getText().toString()); 
      et2.setText(String.valueOf((x*2))); 
     } 
    }); 

    et2.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

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

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

      double x = Double.valueOf(et2.getText().toString()); 
      et1.setText(String.valueOf((x/2))); 
     } 
    }); 
+0

可以use'FocusChangeListener()'本' –

+1

公共無效afterTextChanged(編輯S){如果(mIsCalledFromWatcher!){mIsCalledFromWatcher = TRUE;/*做的東西*/mIsCalledFromWatcher = false; }}'... [看看TextView.setText()](https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/TextView.java#L4211)看發生了什麼... setText導致另一個afterTextChanged調用 – Selvin

+0

你已經構建了一個無限循環 – jr593

回答

1

你可以保持在TextWatcher參考需要在手術前,當後來把他們帶回刪除它們。

private TextWatcher tv1; 
private TextWatcher tv2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    final EditText et1 = (EditText) findViewById(R.id.editText); 
    final EditText et2 = (EditText) findViewById(R.id.editText1); 

    tv1 = new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

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

     @Override 
     public void afterTextChanged(Editable s) { 
      double x = Double.valueOf(et1.getText().toString()); 
      et2.removeTextChangedListener(tv2); // "disable" the watcher 
      et2.setText(String.valueOf((x*2))); 
      et2.addTextChangedListener(tv2); // "enable" the watcher 
     } 
    }; 

    tv2 = new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

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

     @Override 
     public void afterTextChanged(Editable s) { 
      double x = Double.valueOf(et2.getText().toString()); 
      et1.removeTextChangedListener(tv1); // "disable" the watcher 
      et1.setText(String.valueOf((x/2))); 
      et1.addTextChangedListener(tv1); // "enable" the watcher 
     } 
    }; 

    et1.addTextChangedListener(tv1); 
    et2.addTextChangedListener(tv2); 
} 
相關問題