如何从EditText上删除角色? [英] how to remove character from edittext?

查看:136
本文介绍了如何从EditText上删除角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有什么方法,通过它我可以从的EditText 删除特定的字符。我希望通过指定编辑文字的位置,以消除来自的EditText 包机 是有什么样 removeAt()的EditText

I want to know is there any method by which i can remove the specific character from the EditText. I want to remove charter from EditText by specifying position in edit text is there something like removeAt() for EditText?

    public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button b1=(Button)findViewById(R.id.button1);
        final EditText ed1=(EditText)findViewById(R.id.editText1);
        b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

             //i want something like this
                    ed1.removeAt(5);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}


附加信息

主要的问题是,我想使文本加粗内的EditText但不是所有的文字。这是设置在大胆的按钮后,输入的文本(粗体是我的切换按钮)。 确定这里是code

the main problem is i want to make text bold inside edittext but not all the text. The text that is typed after setting bold button on(bold is my toggle button). ok here is the code

     ed1.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            int i = 0;

            switch(event.getAction())
            {
            case KeyEvent.ACTION_UP:
                    if(bold.isChecked())
                    {
                         i=ed1.getSelectionStart();

                         ed1.append(Html.fromHtml("<b>"+ed1.getText().toString().charAt(i-1)+"</b>"));

                    }
                    return true;

            }
            return false;
        }
    });

问题是,我得到双重性格的一个是正常的字符,然后再大胆的,所以我想通过指定有位置,删除普通字符

the problem is that i get double character's one is normal character and then again the bold one so i want to remove that normal characters by specifying there position

推荐答案

您必须将其更改为字符串,首先是因为gettext的返回编辑,如:

You have to change it to string first because getText returns editable, e.g:

int charIndex;
String text = ed1.getText().toString();
text = text.substring(0, charIndex) + text.substring(charIndex+1);
ed1.setText(text);

要删除一个字符,并保持粗体字是相同的:

To remove a Character and keep the BOLD characters the same :

ed1.setText((Spanned)ed1.getText().delete(indexStart , indexEnd));

这将从指数indexStart删除字符为indexEnd -1。 例如如果你使用删除(0,1);它会删除第一个字符。

This will remove the Characters from index "indexStart" to "indexEnd -1". e.g. if you use delete(0,1); it will delete the first Character.

我希望帮助:)

这篇关于如何从EditText上删除角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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