Question

I am trying copy a password from Chrome Browser (in Android) into a EditText but it always copy the format, including two space at the end.

In XML file.

<EditText
    android:id="@+id/signin_etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:layout_marginBottom="5dp"
    android:padding="8dp"/>

My password is

RLqGQQa3

but when I call to getText() it return:

 RLqGQQa3  

with a space at start, and two at end.

These occur only when I copy the password from browser

Était-ce utile?

La solution

Try this..

trim()

To Remove white space characters from the beginning and end of the string.

getText().toString().trim();

Autres conseils

In your class, do this:

String  signin_etPassword = EditTextinput.getText().toString();

signin_etPassword= signin_etPassword.trim();

This will remove only the spaces at the beginning or end of the String. Middle of the password will remain.

Your question is how to paste without format.

This will remove formatting every time you change text, even on paste.

final EditText et = (EditText) findViewById(R.id.yourEditText);
et.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
        }

        public void afterTextChanged(Editable s) {
            CharacterStyle[] toBeRemovedSpans =
                    s.getSpans(0, s.length(), MetricAffectingSpan.class);

            for (int i = 0; i < toBeRemovedSpans.length; i++)
                s.removeSpan(toBeRemovedSpans[i]);

        }

    });

Adapted from this answer: Paste without rich text formatting into EditText

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top