Search code examples
javaandroidstringsubstringtextedit

android textedit delete substring from string on backspace


I have an Android activity that contains a textedit field. The idea is to get the user to enter some text with "blanks" (3 maximum). The blanks are inserted using the function below:

public void insertblank(View view){
    EditText editText = (EditText) findViewById(R.id.editText1);


    int lastIndex = 0;
    int count =0;

    while(lastIndex != -1){

           lastIndex = editText.getText().toString().indexOf("______",lastIndex);

           if( lastIndex != -1){
                 count ++;
                 lastIndex+="______".length();
          }
    }
    if(count <= 2){
        editText.append(" ______ ");
    }else{
        Toast.makeText(getBaseContext(), "Maximum of 3 blanks allowed", Toast.LENGTH_LONG).show();
    }



}

What I need is, as the user backspaces or edits the text the "blanks" need to be treated as one character. So ______ is deleted as soon as backspace is pressed within that sub string.

UPDATE

So with the help of Gabe Sechan's answer I have almost got this working. I've added addTextChangedListener in onCreate and running the following code. This lets replaces all of the underscores in the string and not just the ones at the currentWord.

txt.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // TODO Auto-generated method stub

            Spannable textSpan = txt.getText();
            final int selection = txt.getSelectionStart();
            final Pattern pattern = Pattern.compile("\\w+");
            final Matcher matcher = pattern.matcher(textSpan);
            int end = 0;

            String currentWord = null;
            while (matcher.find()) {
                start = matcher.start();
                end = matcher.end();
                if (start <= selection && selection <= end) {
                    currentWord = textSpan.subSequence(start, end).toString();

                    if(currentWord.contains("_")){
                        String wholeText = txt.getText().toString();
                        Toast.makeText(getApplicationContext(), currentWord+"", Toast.LENGTH_SHORT).show();
                        wholeText = wholeText.replace(currentWord, "");
                        txt.setText(wholeText);  
                        txt.setSelection(txt.getText().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
        }

    });

Solution

  • Your best bet is to put a TextWatcher on the field and look for changes to the blanks in afterTextChanged and if necessary remove the entire blank.