EditText 中的预定义符号

Predefined symbols in an EditText

我使用推荐系统,代码如下:XXXX-XXXX-XXXX

如何在 EditText 中预定义破折号?

试试这个代码:

EditText codeEditText = binding.codeEditText;
final String[] tempString = {""};
final char[][] stringArray = new char[3][0];
codeEditText.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start, int count,int after) {}

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

      if(cardNumber.getText().length()==5 ||cardNumber.getText().length()==10 ||cardNumber.getText().length()==15 ||cardNumber.getText().length()==20) {
         tempString[0] = codeEditText.getText().toString() + "-";
         char c = tempString[0].charAt(tempString[0].length() - 2);

         if (c != '-') {
            stringArray[0] = tempString[0].toCharArray();
            stringArray[0][tempString[0].length() - 2] = stringArray[0][tempString[0].length() - 1];
            stringArray[0][tempString[0].length() - 1] = c;

            tempString[0] = new String(stringArray[0]);
            codeEditText.setText(tempString[0]);
            cardNumber.setSelection(tempString[0].length());
            tempString[0] = null;
         }

      }

   }
});