简体   繁体   English

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

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

I am working on app where an edittext is having $0 as pre-defined text.我正在开发一个应用程序,其中一个编辑文本的预定义文本为 $0。 Zero is editable.零是可编辑的。 But as soon as user enters an amount like $80, its showing $080.但是一旦用户输入 80 美元这样的金额,它就会显示 080 美元。 I am using textwatcher to non deleted $ and replaced at time of printing value.我正在使用 textwatcher 来非删除 $ 并在打印值时替换。

How can i achieve the output as $80 when user is typing an amount当用户输入金额时,我如何获得 80 美元的输出

preset value - $0 after typing $80, Output = $080 Expected = $80预设值 - 输入 $80 后 $0,输出 = $080 预期 = $80

Thanks in advance.提前致谢。

   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());

            }

        }
    });

Instead of using the text property you should HINT 而不是使用text属性,您应该提示

amountEdittext.setHint("$0");

you will no longer require to any other code. 您将不再需要任何其他代码。 and keep the text blank you can then add validation to check for empty text. 并将文本保留为空白,然后可以添加验证以检查是否为空文本。

you can change the hint when it has focus using setOnFocusChangeListener() 您可以使用setOnFocusChangeListener()更改提示的焦点

Just pass the Edittext clear. 只需将Edittext清除。 When user clicks on edittext just remove the value 当用户单击edittext时,只需删除该值

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

or Something like this 或类似的东西

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

Better use a InputFilter than using TextWatcher . 最好使用InputFilter比使用TextWatcher Since you've the $ sign better to remove it from the EditText and use one TextView along with EditText horizontally. 由于您最好使用$号将其从EditText删除,并水平使用一个TextViewEditText So that you can get rid of the dollar sign 这样就可以摆脱美元符号

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 ""
    }
}

I have just realized what you are doing, Selection.setSelection moves the cursor to the end of text in the edittext,so that will be after zero. 我刚刚意识到您在做什么, Selection.setSelection将光标移动到edittext中文本的末尾,因此它将在零之后。

So, here is a solution that i think would work; 因此,这是我认为可行的解决方案;

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

This should solve your question. 这应该可以解决您的问题。

    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