我只是在项目中与Kotlin玩耍,但遇到了一个奇怪的问题...当尝试转换自定义EditText
时,Android Studio停止响应。当我尝试部分转换时,转换这段代码时它停止响应:
private TextWatcher editor = new TextWatcher() {
long newMicro = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
s = s.toString().replace(".", "")
.replace(",", "")
.replace("$", "");
try {
newMicro = Long.parseLong(s.toString());
} catch (Exception e) {
newMicro = 0;
}
}
@Override
public void afterTextChanged(Editable editable) {
removeTextChangedListener(editor);
setMicroUnits(newMicro);
addTextChangedListener(editor);
}
};
您是否经历过这种行为?我无法在Kotlin中重新实现此
TextWatcher
,因为我无法执行任何操作CharSequence.toString().replace()
也不
CharSequence.replace()
知道如何在Kotlin中实现自定义TextWatcher吗? 这是我准备的代码:
val editor = object : TextWatcher {
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {
}
}
编辑:在Android Studio 3 Preview 6上使用kotlin版本
1.1.2-4
时出现问题 最佳答案
因此,看起来不仅是我的问题,而且转换时TextWatcher
挂起。每次都停止响应。我的问题是,我无法使用Kotlins String.replace()
解决方案只是使用正确版本的Kotlin
带有kotlin_version = '1.1.3-2'
的AndroidStudio 3.0预览版6
关于android - 具有CharSequence/String替换功能的Kotlin和Android TextWatcher,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45043391/