“对飞”风格的EditText内容?风格、内容、EditText

2023-09-04 05:45:20 作者:夜の未殇

我工作在Android的富文本编辑器。基本上它有粗体,斜体和链接按钮,都连接到一个EditText来改变内容的风格。我有,如果你选择你想先样式的文字,然后用这个方法选择按钮,它伟大的工作:http://developer.android.com/guide/appendix/faq/commontasks.html#selectingtext.

I'm working on a rich text editor in Android. Basically it has bold, italics and link buttons that are tied to an EditText to change the style of the content. I have it working great if you select the text you want to style first, and then select the button using this method: http://developer.android.com/guide/appendix/faq/commontasks.html#selectingtext.

我想要做的是有它的工作就像一个富文本编辑器,在这里你可以使用这些按钮间的切换样式的文本,只要你想,然后再次点击切换到停止使用样式。所以,如果我想输入注意这个!粗体,就点击'B'按钮,然后开始打字我型将是大胆的文字和一切,直到我点击'B 再次按钮。

What I'm trying to do is have it work like a rich text editor, where you can use the buttons as a toggle to style the text for as long as you'd like, then click the toggle again to stop using the style. So if I wanted to type 'Pay attention to this!' in bold, I would click the 'B' button, then start typing the text and everything I type would be bold until I click the 'B' button again.

如何把这事办成任何想法?我希望我已经清楚不过了:)

Any ideas on how to pull this off? I hope I've been clear enough :)

推荐答案

您可以只是每个字符后,他们键入使用更新的样式TextWatcher而addTextChangedListener()方法。

You could just update the style after every character that they type using a TextWatcher and the addTextChangedListener() method.

没关系,这仅仅是裸露的骨头例如code。

Ok, this is just the bare bones example code.

int mStart = -1;

// Bold onClickListener
public void onClick(View view)
{
    if(mStart == -1) mStart = mEditText.getText().length();
    else mStart = -1;
}

// TextWatcher
onTextChanged(CharSequence s, int start, int before, int count)
{
    if(mStart > 0)
    {
        int end = mEditText.getText().length();
        mEditText.getText().setSpan(new StyleSpan(android.graphics.Typeface.BOLD), mStart, end - mStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}