maddygoround
maddygoround

Reputation: 2300

how to remove character from 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;
    }


}

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

Upvotes: 8

Views: 20682

Answers (4)

Ajit Kumar Dubey
Ajit Kumar Dubey

Reputation: 1563

My Implementation is given below. I hope it will be helpful

int cursorPosition = mDialerEditText.getSelectionStart();
    if (cursorPosition > 0) {
          mDialerEditText.setText(mDialerEditText.getText().delete(cursorPosition - 1, cursorPosition));
        mDialerEditText.setSelection(cursorPosition-1);
    }

Upvotes: 6

Eddy K
Eddy K

Reputation: 874

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

Upvotes: 12

Swayam
Swayam

Reputation: 16364

Get the input from the EditText and store in a String.

String temp = ed1.getText().toString();

Now manipulate this String temp to remove the character at the desired position. (I suggest you use String.substring(..) function to achieve this.)

Lastly, set the manipulated string temp in the EditText.

ed1.setText(temp);

Upvotes: 0

RE6
RE6

Reputation: 2724

EditText is just like any other TextView, but it can also be edited. Therefore you can use the getText() method, store it as a String, remove the desired char, and call setText(str) method on the EditText to change the text.

Upvotes: 1

Related Questions