繁体   English   中英

当用户在编辑文本中输入金额时,用金额替换 0 的最佳方法

[英]Best approach to replace 0 with amount, when user enters an amount in edittext

我正在开发一个应用程序,其中一个编辑文本的预定义文本为 $0。 零是可编辑的。 但是一旦用户输入 80 美元这样的金额,它就会显示 080 美元。 我正在使用 textwatcher 来非删除 $ 并在打印值时替换。

当用户输入金额时,我如何获得 80 美元的输出

预设值 - 输入 $80 后 $0,输出 = $080 预期 = $80

提前致谢。

   amountEdittext.setText("$0");
    Selection.setSelection(amountEdittext.getText(), 
 amountEdittext.getText().length());
    amountEdittext.addTextChangedListener(new TextWatcher() {

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

        }

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

        @Override
        public void afterTextChanged(Editable s) {
            if (!s.toString().startsWith("$")) {
                amountEdittext.setText("$");
                Selection.setSelection(amountEdittext.getText(), amountEdittext.getText().length());

            }

        }
    });

而不是使用text属性,您应该提示

amountEdittext.setHint("$0");

您将不再需要任何其他代码。 并将文本保留为空白,然后可以添加验证以检查是否为空文本。

您可以使用setOnFocusChangeListener()更改提示的焦点

只需将Edittext清除。 当用户单击edittext时,只需删除该值

amountEdittext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        amountEdittext.getText().clear();
    }
});

或类似的东西

@Override
    public void afterTextChanged(Editable s) {
        String myValue = "$"+amountEdittext.getText().toString();
    }

最好使用InputFilter比使用TextWatcher 由于您最好使用$号将其从EditText删除,并水平使用一个TextViewEditText 这样就可以摆脱美元符号

class DecimalDigitsInputFilter(digitsBeforeZero: Int, digitsAfterZero: Int) : InputFilter {
private val mPattern: Pattern

init {
    mPattern =
        Pattern.compile("[0-9]{0," + (digitsBeforeZero ) + "}+((\\.[0-9]{0," + (digitsAfterZero ) + "})?)||(\\.)?")
}

override fun filter(
    source: CharSequence?,
    start: Int,
    end: Int,
    dest: Spanned?,
    dstart: Int,
    dend: Int
): CharSequence? {

    if (source != null && dest != null) {
        val replacement = source.subSequence(start, end).toString()
        val newVal = (dest.subSequence(0, dstart).toString() + replacement
                + dest.subSequence(dend, dest.length).toString())
        val matcher = mPattern.matcher(newVal)
        if (matcher.matches())
            return null

        return if (TextUtils.isEmpty(source))
            dest.subSequence(dstart, dend)
        else
            return ""
    }
    return ""
    }
}

我刚刚意识到您在做什么, Selection.setSelection将光标移动到edittext中文本的末尾,因此它将在零之后。

因此,这是我认为可行的解决方案;

    amountEdittext.setText("$0");
    amountEdittext.setOnFocusChange(View view, boolean b) {
        if (b && amountEdittext.getText().toString().matches("^\\$0$")) {
            amountEdittext.setText("$");
            Selection.setSelection(amountEdittext.getText(), amountEdittext.getText().length());
        } else {
            Selection.setSelection(amountEdittext.getText(), amountEdittext.getText().length());
        }
    }

这应该可以解决您的问题。

    amountEdittext.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (editable.length() > 2) {
                for (int i = 0; i < editable.length() - 1; i++) {
                    if (editable.charAt(i) == '$' && editable.charAt(i + 1) == '0') {
                        editable.delete(i + 1, i + 2);
                    }
                }
            }
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM