如何从编辑文本中删除字符?[英] how to remove character from edittext?

本文是小编为大家收集整理的关于如何从编辑文本中删除字符?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想知道有没有任何方法,我可以从中删除EditText的特定字符.我想通过在编辑文本中指定位置来删除EditText的章程 是否有类似removeAt()的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内部制作文本粗体,但不是所有的文本.在设置粗体按钮(粗体)后键入的文本(粗体是我的切换按钮). 好的,这是代码

     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;
        }
    });

问题是我得到双字符的一个是普通的字符,然后再次粗体,所以我想通过指定位置

来删除该普通字符

推荐答案

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

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

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

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

这将从索引"indexstart"中的字符中删除到"indexend -1". 例如如果使用删除(0,1);它将删除第一个字符.

我希望有帮助:)

本文地址:https://www.itbaoku.cn/post/102317.html

问题描述

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;
    }


}

EXTRA INFORMATION

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

推荐答案

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));

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.

I hope that helps :)