class NoCopySpanEditableFactory(private vararg val spans: NoCopySpan): Editable.Factory() { override fun newEditable(source: CharSequence): Editable { return SpannableStringBuilder.valueOf(source).apply { spans.forEach { setSpan(it, 0, source.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE) } } } } 转成java代码
时间: 2024-01-07 18:02:53 浏览: 191
以下是对应的 Java 代码:
```
import android.text.Editable;
import android.text.NoCopySpan;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
public class NoCopySpanEditableFactory extends Editable.Factory {
private NoCopySpan[] spans;
public NoCopySpanEditableFactory(NoCopySpan... spans) {
this.spans = spans;
}
@Override
public Editable newEditable(CharSequence source) {
SpannableStringBuilder builder = SpannableStringBuilder.valueOf(source);
for (NoCopySpan span : spans) {
builder.setSpan(span, 0, source.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
return builder;
}
}
```
注意,Kotlin 中的 `apply` 函数在 Java 中没有对应的语法,因此需要使用普通的 for 循环来设置 Span。
阅读全文