Android edittext append and remove append

风流意气都作罢 提交于 2019-12-11 07:59:48

问题


I'm trying to make editText, where I am inserting some text. After each three characters,I want to insert dash.

Example:

Type: 123

Result:123-

Now when cursor is behind dash and you press delete, I want to delete dash and character behind dash. For example:

123-

result after delete key: 12. How to do it. Thank you for advice.

EDIT

my code is:

 EditText editText;
boolean keyDel = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText);
    editText.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DEL) {
                keyDel = true;
            }
            return keyDel;
        }
    });

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String str = s.toString();
            System.out.println(str.length());
            if (str.length() == 3) {
                str = str + "-";
            } else if (str.length() == 7) {
                str = str + "-";
            } else if (str.length() % 4 == 0 && keyDel == true) {
                str = str.substring(0, str.length() - 2);
            } else {
                return;
            }
            editText.setText(str);
            editText.setSelection(editText.getText().length());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

}

I found Android 4.4.2 and higer doesn´t support keyevent.


回答1:


I was inspired by this answer to achieve what you want:

    String mTextValue;
    Character mLastChar = '\0'; // init with empty character
    int mKeyDel;


    myEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

           boolean flag = true;
           String eachBlock[] = myEditText.getText().toString().split("-");
           for (int i = 0; i < eachBlock.length; i++) {
                if (eachBlock[i].length() > 4) {
                    flag = false;
                }
           }
           if (flag) {

              myEditText.setOnKeyListener(new View.OnKeyListener() {

               @Override
               public boolean onKey(View v, int keyCode, KeyEvent event) {

                      if (keyCode == KeyEvent.KEYCODE_DEL)
                            mKeyDel = 1;
                            return false;
                      }
                });

               if (mKeyDel == 0) {

                  if (((myEditText.getText().length() + 1) % 4) == 0) {
                      myEditText.setText(myEditTex.getText() + "-");
                      myEditText.setSelection(myEditText.getText().length());
                  }
                  mTextValue = myEditText.getText().toString();
               } else {
                  mTextValue = myEditText.getText().toString();
                  if (mLastChar.equals('-')) {
                       mTextValue = mTextValue.substring(0, mTextValue.length() - 1);
                       myEditText.setText(mTextValue);
                       myEditText.setSelection(mTextValue.length());
                  }
                  mKeyDel = 0;
               }

          } else {
              myEditText.setText(mTextValue);
          }

       }

       @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (s.length()>0) {// save the last char value
                mLastChar = s.charAt(s.length() - 1);
            } else {
                mLastChar = '\0';
            }
       }

       @Override
       public void afterTextChanged(Editable s) {}

  });



回答2:


onTextChanged is called everytime you add and remove something. So if your String has length 3, you add your - and the new length is 4. If you press delete (new length is 3 again), onTextChanged is called and - is added again. SO only add something if nothing has been removed from the text.

if (count > before) {
         if (count == 3 || count == 7) {
             str = str + "-";
         } else {
             return;
         }
         input.setText(str);
         input.setSelection(input.getText().length());
}



回答3:


I'm rather new to android programming myself. Nevertheless, I think that everytime you call "setText" you are trigging a new onTextChanged event. In my app I remove the listener, set the text and then add the listener again in order to avoid this problem. But for doing this you'll have to save the reference to the TextWatcher.

I.e. given your Activity extends TextWatcher:

edittext.removeTextChangedListener(this);
editText.setText(str);
edittext.addTextChangedListener(this);

You can also put the cursor at the end using:

myedittext.append("");


来源:https://stackoverflow.com/questions/33024891/android-edittext-append-and-remove-append

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!