样式EditText内容'在飞行'? [英] Style EditText content 'on the fly'?

查看:165
本文介绍了样式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 :)

推荐答案

你可以更新每个字符后, a href =http://developer.android.com/intl/fr/reference/android/text/TextWatcher.html =nofollow noreferrer> TextWatcher addTextChangedListener() 方法。

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

好吧,这只是一个裸骨骼示例代码。 p>

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