How is EasyEditSpan used in Android for partial text editing?

I want to allow the user to edit part of the text in an Android application. I see a class called EasyEditSpan, but when I insert it into the TextView, nothing happens. I tried to edit the TextView, but it still has no effect. If you switch to EditText, the entire line of text is edited, which is also incorrect. Here is my code:

@Override public void onCreate(Bundle savedInstanceState) { TextView testView = (TextView)findViewById(R.id.text_view); testView.setText(buildMiddleEditSpannable("Please enter your ", "Name", " here.")); } public static Spannable buildMiddleEditSpannable(CharSequence beginning, CharSequence middle, CharSequence end) { int spanMidStart = beginning.length(); int spanMidEnd = spanMidStart + middle.length(); SpannableString span = new SpannableString(new StringBuilder(middle).insert(0, beginning).append(end)); span.setSpan(new EasyEditSpan(), spanMidStart, spanMidEnd, 0); return span; } 
+7
source share
2 answers

After viewing the code for the frame related to EasyEditSpan ( EasyEditSpan , TextView, and TextUtils ), it becomes obvious that even in what he says in his description:

Provides an easy way to edit parts of text.

The functionality currently available is limited only to the second part of the description as follows:

TextView uses this range to allow the user to delete a text cartridge with one click. text. TextView deletes this range as soon as the text is edited or the cursor moves.

Here is an example quick code example that demonstrates its use:

 public class EasyEditSpanActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final EditText editText = new EditText(this); setContentView(editText); showToast("Longclick to set EasyEditSpan for the line on cursor"); editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); editText.setSingleLine(false); editText.setText("####\n#######\n###\n######\n####".replace("#", "text ")); editText.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { final Layout layout = editText.getLayout(); final int line = layout.getLineForOffset(editText.getSelectionStart()); final int start = layout.getLineStart(line); final int end = layout.getLineEnd(line); editText.getEditableText().setSpan(new EasyEditSpan(), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); showToast("Edit line to show EasyEdit window"); return true; } }); } private void showToast(String message) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); } } 

So, unfortunately, if you need a way to allow the user to edit only part of the text in the application, EasyEditSpan doesn't seem to help much. You will probably need to implement some code using ClickableSpan and possibly a custom dialog.

+5
source

You might be better off using ClickableSpan and a popup dialog for editing content. However, I tried to implement partially editable text. This is not complete and may have some rough edges. It uses three spans and overrides the onSelectionChanged (int selStart, int selEnd) and onKeyDown (int keyCode, KeyEvent event) methods to disable editing, while the target is part1 or part3. Hope this helps.

//activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.sensei.partialedit.EditTextSelectable android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:text="@string/hello_world" android:background="#00000000" tools:context=".MainActivity" /> </RelativeLayout> 

//MainActivity.java

 package com.sensei.partialedit; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditTextSelectable testView = (EditTextSelectable) findViewById(R.id.text_view); testView.setText("Please enter your ", " Name ", " here."); } } 

//EditTextSelectable.java

 package com.sensei.partialedit; import android.content.Context; import android.graphics.Color; import android.text.Spannable; import android.text.SpannableStringBuilder; import android.text.method.KeyListener; import android.text.style.CharacterStyle; import android.text.style.ForegroundColorSpan; import android.text.style.UnderlineSpan; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.widget.EditText; public class EditTextSelectable extends EditText { private String part1; private String part2; private String part3; private ForegroundColorSpan span1; private CharacterStyle span2; private ForegroundColorSpan span3; public EditTextSelectable(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setTag(getKeyListener()); } public EditTextSelectable(Context context, AttributeSet attrs) { super(context, attrs); setTag(getKeyListener()); } public EditTextSelectable(Context context) { super(context); setTag(getKeyListener()); } public void setText(String part1, String part2, String part3) { setText(buildMiddleEditSpannable(part1, part2, part3)); setSelection(part1.length() + part2.length() - 1); } private Spannable buildMiddleEditSpannable(String part1, String part2, String part3) { this.part1 = part1; this.part2 = part2; this.part3 = part3; SpannableStringBuilder spannable = new SpannableStringBuilder(part1 + part2 + part3); span1 = new ForegroundColorSpan(Color.GRAY); spannable.setSpan(span1, 0, part1.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); span2 = new UnderlineSpan(); spannable.setSpan(span2, part1.length(), part1.length() + part2.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); span3 = new ForegroundColorSpan(Color.GRAY); spannable.setSpan(span3, (part1 + part2).length(), (part1 + part2 + part3).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spannable; } @Override protected void onSelectionChanged(int selStart, int selEnd) { if (part1 == null) return; if (selStart >= getText().getSpanStart(span2) && selEnd <= getText().getSpanEnd(span2)) { setKeyListener((KeyListener) getTag()); setCursorVisible(true); } else { setKeyListener(null); setCursorVisible(false); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode==KeyEvent.KEYCODE_ENTER) { // Just ignore the [Enter] key return true; } int selectionStart = getSelectionStart(); int selectionEnd = getSelectionEnd(); Log.d("partialedit", "OnKeyDown:" + selectionStart + ":" + selectionEnd + ":" + getText().getSpanStart(span2) + ":" + getText().getSpanEnd(span2)); if (selectionStart < part1.length() + 1 || selectionStart >= getText().length() - part3.length()) { return true; } // Handle all other keys in the default way return super.onKeyDown(keyCode, event); } } 
+3
source

All Articles