中的EditText选择文本 [英] select text in to edittext

查看:235
本文介绍了中的EditText选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序。在我的应用我面临着一个奇怪的problem.Please有我的屏幕上看看。

I am working on an Android application. In my app I am facing a strange problem.Please have a look on my screen.

当我选择selction框中的任何东西,然后将两次添加到EditText上。

when i select any thing from selction box then it will add twice to the edittext.

以下是我的code代表的EditText

the following is my code for edittext

snd_txt.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            System.out.println("onTextChanged"+start+"b4"+before+"count"+count);
            if (start>0) {

                    int m=snd_txt.getText().toString().lastIndexOf(".");
                    makeColorText(m,snd_txt.getText().toString().length());

            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });




public void makeColorText(int num,int last) {

    int start=last-44;
    if (start<1){
        start=0;
    }
     SpannableString ss = new SpannableString(snd_txt.getText());

     ss.setSpan(new ForegroundColorSpan(R.color.Gray_Light), 0, start,0);
     ss.setSpan(new ForegroundColorSpan(Color.BLACK), start, last, 0); 
    // customize ss here
    // ...



    snd_txt.setText(ss);
    snd_txt.setSelection(last);
}

如果我评论的 snd_txt.setText(SS); 语句.the的EditText上会很好地工作。但我不能做that.I使用该方法在运行时改变颜色的EditText。在选择predictions会加字两倍的时间做工精细所有time..But。

If i comment the snd_txt.setText(ss); statement .the the edittext will work perfectly. But I can't do that.I used that method to change edittext color at runtime. working fine for all the time..But at the time of selecting predictions will add the word two times.

任何人有任何想法去解决,请大家帮帮我。

Anybody have any idea to solve, Please help me.

编辑:如何创建多色文字定制的EditText。对于如:蓝色和previous当前行的black.something像

How can I create a custom edittext with multicolor text.. for eg: current line in blue and previous are black.something like that

推荐答案

通过更改我的code解决我的问题。

Solved my problem by changing my code.

snd_txt.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if (start>0) {
                 int m=snd_txt.getText().toString().lastIndexOf(".");
         makeColorText(snd_txt.getText().toString().length(),count);    
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });

public void makeColorText(int last,int count) {

    int start=last-44;
    if (start<1){
        start=0;
    }

    String s=snd_txt.getText().toString();

    if (count==before_val&count!=1) {

        s=s.substring(0, last-before_val);
        last=last-before_val;
    }

     SpannableString ss = new SpannableString(s);

     ss.setSpan(new ForegroundColorSpan(R.color.Gray_Light), 0, start,0);
     ss.setSpan(new ForegroundColorSpan(Color.BLACK), start, last, 0); 
    // customize ss here
    // ...



         snd_txt.setText(ss);


     before_val=count;


    snd_txt.setSelection(last);
}

这篇关于中的EditText选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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