Android的开发:如何更换一个EditText的一部分,一个Spannable [英] Android Development: How To Replace Part of an EditText with a Spannable

查看:145
本文介绍了Android的开发:如何更换一个EditText的一部分,一个Spannable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更换为可编辑的一部分Gettext中的(),跨度返回。

I'm trying to replace part of an Editable returned from getText() with a span.

我试过的getText()代替(),但是这仅适用于CharSequences。

I've tried getText().replace() but that's only for CharSequences.

究其原因,我想,而不是去通过,并强调整个的EditText(可与大文件速度慢),要做到这一点,所以我可以突出一个EditText此起彼伏部分(后小的延迟)。

The reason I'm trying to do this is so I can highlight sections of an EditText one after another (after a small delay), instead of going through and highlighting the whole EditText (which can be slow with big files).

有没有人有一个线索我怎么会去这样做?

Does anyone have a clue about how I'd go about doing this?

推荐答案

这个最小尺寸的例子,使单词的第一个'大:

This minimal size example makes the word 'first' large:

public class SpanTest extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String dispStr = "I'm the first line\nI'm the second line";
        TextView tv = (TextView) findViewById(R.id.textView1);
        int startSpan = dispStr.indexOf("first");
        int endSpan = dispStr.indexOf("line");
        Spannable spanRange = new SpannableString(dispStr);
        TextAppearanceSpan tas = new TextAppearanceSpan(this, android.R.style.TextAppearance_Large);
        spanRange.setSpan(tas, startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(spanRange);
    }
}

您可以使其适应您的需求。

You can adapt it to your needs

这篇关于Android的开发:如何更换一个EditText的一部分,一个Spannable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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