¿Cómo agregar $ cuando el usuario ingresa cualquier cantidad en el texto de edición?

I my application when user enter any numberic value in EditText then $ append() before text. Any help would be greatly appreciated...thankss a lot...

preguntado el 28 de mayo de 14 a las 12:05

I have used TextWatcher but it is not working -

You want to display the $ sign on the screen as they type? -

you did it wrong way then -

@NeetuShrivastava Show us code what you have tried so far. -

I used this code :public void afterTextChanged(Editable editable) { // final EditText editText = (EditText) view; editable.append("$"); MainActivity.this.edit.setText(editable); } Application crashes. -

2 Respuestas

Intente usar un TextWatcher como sigue:

EditText editbox = (EditText)findViewById(R.id.editText1);
editbox.addTextChangedListener(new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s){

            if(s.toString().length() > 0 && s.toString().charAt(0) != '$'){
                StringBuilder b = new StringBuilder(s.toString());
                b = b.reverse();
                b.append('$');
                b = b.reverse();
                editbox.setText(b);
                editbox.setSelection(editbox.getText().length());
            }



            int counter = 0;
            for (int i = 0; i < s.toString().length(); i++) {
                if (s.toString().charAt(i) == '$') {
                    counter++;
                }
            }
            if (counter > 1) {
                String str = s.toString().replace("$", "").trim();
                str = str.replaceAll("\\s+", "");
                StringBuilder b = new StringBuilder(str);
                b = b.reverse();
                b.append('$');
                b = b.reverse();
                edit.setText(b);
                edit.setSelection(edit.getText().length());
            } else {

                int spaceCount = 0;

                for (int i = 0; i < s.toString().length(); i++) {
                    if (s.toString().charAt(i) == ' ') {
                        spaceCount++;
                    }
                }
                if (spaceCount > 0) {
                    String str = s.toString().replace("$", "").trim();
                    str = str.replaceAll("\\s+", "");
                    StringBuilder b = new StringBuilder(str);
                    edit.setText(b);
                    edit.setSelection(edit.getText().length());
                }
            }


        }
    });

Another approach would be to combine a TextWatcher with a regular expression of some sort.

Respondido el 14 de junio de 19 a las 13:06

Una línea editbox.setText("$"+editbox.getText().toString()); would be fine - Girish Nair

That would add the "$" symbol to the text every time the text changes... ;) - Yash Sampat

This should put $ sign in each and every character. - Hardik Joshi

I am saying instead of your lines from StringBuilder b = new StringBuilder(s.toString()); b = b.reverse(); b.append('$'); b = b.reverse(); editbox.setText(b); una sola línea de editbox.setText("$"+editbox.getText().toString()); would have been fine - Girish Nair

@GirishNair This single line not work. onTextChanged() is not a place to modify text of EditText. - Hardik Joshi

Usa el siguiente código

ed.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if(ed.getText().toString().length()>1)
            {
                ed.setText("$"+ed.getText().toString());
            }

        }
    });

contestado el 28 de mayo de 14 a las 12:05

changing the text inside textwatcher is that recommended?? - Argumento ilegal

Ésta no es la solución. - Hardik Joshi

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.