var slTextwatcher: TextWatcher? = null
var jgTextwatcher: TextWatcher? = null
slTextwatcher = object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
etJg.clearTextChangedListeners()
if (charSequence.toString().isNotEmpty() && charSequence.toString().trim().toDouble() > 0.0) {
etJg.setText(charSequence.toString().trim().toDouble() * 7.00)
} else {
etJg.setText("")
}
}
override fun afterTextChanged(editable: Editable) {
etJg.addTextChangedListener(jgTextwatcher);
}
}
jgTextwatcher = object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {
etSl.clearTextChangedListeners()
if (charSequence.toString().isNotEmpty() && charSequence.toString().trim().toDouble() > 0.0) {
etSl.setText(charSequence.toString().trim().toDouble() / 7.00)
} else {
etSl.setText("")
}
}
override fun afterTextChanged(editable: Editable) {
etSl.addTextChangedListener(slTextwatcher);
}
}
etSl.addTextChangedListener(slTextwatcher);
etJg.addTextChangedListener(jgTextwatcher);
(转载)工具类代码如下:
import android.content.Context;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
import java.util.ArrayList;
/**
* @created: by Android Studio.
* @describe: ExtendedEditText
*/
public class ExtendedEditText extends EditText {
private ArrayList<TextWatcher> mListeners = null;
public ExtendedEditText(Context ctx) {
super(ctx);
}
public ExtendedEditText(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}
public ExtendedEditText(Context ctx, AttributeSet attrs, int defStyle) {
super(ctx, attrs, defStyle);
}
@Override
public void addTextChangedListener(TextWatcher watcher) {
if (mListeners == null) {
mListeners = new ArrayList<TextWatcher>();
}
mListeners.add(watcher);
super.addTextChangedListener(watcher);
}
@Override
public void removeTextChangedListener(TextWatcher watcher) {
if (mListeners != null) {
int i = mListeners.indexOf(watcher);
if (i >= 0) {
mListeners.remove(i);
}
}
super.removeTextChangedListener(watcher);
}
public void clearTextChangedListeners() {
if (mListeners != null) {
for (TextWatcher watcher : mListeners) {
super.removeTextChangedListener(watcher);
}
mListeners.clear();
mListeners = null;
}
}
}