样式 EditText 内容'即时'? [英] Style EditText content 'on the fly'?

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

问题描述

我正在开发 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 :)

推荐答案

您可以在他们键入的每个字符之后使用 TextWatcheraddTextChangedListener() 方法.

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

好的,这只是简单的示例代码.

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天全站免登陆