Reputation: 34499
I'm experiencing issues trying to use a custom type that implements Editable
as the basis for an EditText
. Here is my code:
// This is a custom type that implements Editable.
// It's just a wrapper over a SpannableStringBuilder, with some
// irrelevant added functionality.
public class ColoredText implements Editable {
private final SpannableStringBuilder builder;
public ColoredText(String text) {
this.builder = new SpannableStringBuilder(text);
}
// All Editable methods are implemented through this.builder.
@Override
public Editable replace(int i, int i1, CharSequence charSequence, int i2, int i3) {
this.builder.replace(i, i1, charSequence, i2, i3);
return this;
}
...
}
public class NoCopyEditableFactory extends Editable.Factory {
@Override
public Editable newEditable(CharSequence source) {
return (Editable) source;
}
}
// In MainActivity.java
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
String line = "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
String text = TextUtils.join("\n", Collections.nCopies(100, line));
Editable e = new ColoredText(text);
EditText et = (EditText) findViewById(R.id.editText);
et.setEditableFactory(new NoCopyEditableFactory()); // This line is causing trouble.
et.setText(e, TextView.BufferType.EDITABLE);
}
// In the MainActivity layout file
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
For some reason, when I comment out the NoCopyEditableFactory
, the cursor updates just fine. However, when I uncomment the line forcing the EditText
not to copy the ColoredText
into a new SpannableStringBuilder
, the cursor does not update when I click on new places in the text. Why is this?
Upvotes: 0
Views: 157
Reputation: 34499
The problem turned out to be with my Editable
implementation. It works fine after I deleted some misbehaving code.
Upvotes: -1