Android的EDITTEXT追加和删除的append

Android的EDITTEXT追加和删除的append

本文介绍了Android的EDITTEXT追加和删除的append的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让EDITTEXT,我在哪里插入一些文本。每三个大字后,我想插入冲刺。

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

例如:

类型:123

结果:1​​23 -

Result:123-

现在,当光标背后是短跑和你preSS删除,我想删除破折号和字符后面冲刺。
例如:

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

123 -

结果后删除键:12。
怎么做。谢谢你的建议。

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

修改

我的code是:

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

}

我发现的是Android 4.4.2和海格doesn't支持的KeyEvent。

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

推荐答案

我被这个答案达到什么你想要的:

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

  });

这篇关于Android的EDITTEXT追加和删除的append的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 02:55