Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm implementing EditText's and I'm facing with one issue... I've put that the inputType is numberDecimal but at the time I type on the EditText I'd like to put the "." or "," autommatically... I can put for example 100.00 or 90.00 it depends of the user... Can it be possible?

I tried to create a TextWatcher and do some things on onTextChanged like see if the charSequence-lenght()==2 add a "." or "," but doesn't work...

Can I detect when to put the comma or dot even if I have a decimal format like ##.## or ###.## ? As I said it can be 100.00 or 80.00.

Edit

 @Override
    public void afterTextChanged(Editable editable) {
        //et1 and et2 are not empty
        if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){
            double pe1 , pe2;
            pe1 = Double.parseDouble(et1.getText().toString());
            pe2 = Double.parseDouble(et2.getText().toString());
            double res = (pe1+pe2)/2;
            String formattedString = String.format("%,.2f", res);
            tvPr.setText(formattedString);

        }

The problem is on the textInput not on afterTextChanged I guess... because I have to put the "." manually and I want to avoid this.

EDIT2

Also a friend of mine suggested to me that I could ignore the . and , and since the marks are always 100, 80, 32, etc... on the editText just type those numbers but then tract those as a double and I'd like to put a ,00 when the user finish the editting of editText is that possible?

Are the comparation if(et1.getText().toString().length() == 5 && et2.getText().toString().length() == 5){ good at the time to check wheter is an edittext not empty? I have to update a TextView once both edittext are not empty and do some operations so, I tried this way, is there another one better? (I put lenght()==5 because on EditText I put maxlenght() = 5;

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
142 views
Welcome To Ask or Share your Answers For Others

1 Answer

All you have to do is something like this

final DecialFormat df = new DecimalFormat("#.00");

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

    @Override
    public void afterTextChanged(Editable s) {
        String value = s.toString();
        if(TextUtils.isEmpty(value)){
            // do what you want if it is empty
            return;
        }

        try {
            DecimalFormat df = new DecimalFormat("#.00");
            String testFormat = df.format(Double.parseDouble(s.toString()));

            // Manage the cursor position
            int newPos;
            try {
                int pos = et.getSelectionStart();
                int sizeDiff = testFormat.length() - s.length();
                newPos = pos + sizeDiff;
            }catch(Exception e1){
                e1.printStackTrace();
                newPos = testFormat.length();
            }

            // Modify the edit text to hold the double value
            et.removeTextChangedListener(this);
            et.setText(testFormat);
            et.setSelection(newPos);
            et.addTextChangedListener(this);
        }catch(Exception e){
            e.printStackTrace();
            et.removeTextChangedListener(this);
            et.setText("");
            et.addTextChangedListener(this);
        }
    }
}

Modify the above code to do what you need, but this is the basic idea behind making sure the value is of decimal form. It is worth mentioning that when you modify text like this the cursor position is always a "fun" thing to try to get how you want it. What happens if you have "100.00" and someone puts the cursor in a place like "100.0|0" and presses 5. Should the result be "100.0|5" or "100.05|" and if it is the latter how do you handle them putting in the next value, do you use it to round the previous value? It is an interesting situation, but this will get you started.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...