“对飞”风格的EditText内容? [英] Style EditText content 'on the fly'?

查看:92
本文介绍了“对飞”风格的EditText内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

您可以只是每个字符后,他们键入使用更新的样式<一个href="http://developer.android.com/intl/fr/reference/android/text/TextWatcher.html"><$c$c>TextWatcher而<一href="http://developer.android.com/intl/fr/reference/android/widget/TextView.html#addTextChangedListener%28android.text.TextWatcher%29"><$c$c>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);
    }
}

这篇关于“对飞”风格的EditText内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆