简体   繁体   中英

Just One character is shown in edit text field in java android

I am trying to do login in android app in java, but only one character is shown in editText field (I can input just one character when I'm trying to input the second character the first one is gone), the password field works well, this problems not occurs in all phones just in some. All The phones are all android 9 and 8, the difference is in screens resolution.

I got the problem in different screens resolution, not only one type. I can write all the string but only when I return the cursor to the left: the string is shown inversely ( for example if I try to input hello it will show: olleh)

What can be the source of problem please?

Name field:

<com.google.android.material.textfield.TextInputLayout

    android:id="@+id/userWrapper"
    style="@style/MessageError"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="2dp"
    android:textAlignment="center"
    android:layout_marginBottom="5dp"
    android:gravity="center_horizontal">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:id="@+id/user_edit_text"
        android:hint="Name"
        android:background="@drawable/custom_input"
        android:padding="10dp"
        android:paddingStart="20dp"
        />
    <ImageButton
        android:id="@+id/clean"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_baseline_cancel_24"
        android:layout_centerVertical="true"
        android:layout_marginTop="2dp"
        android:layout_marginLeft="-40dp"
        android:background="@color/white_with_alpha"
        android:onClick="cleanText"
        android:visibility="gone"
        android:text="Button"/>
    </LinearLayout>
</com.google.android.material.textfield.TextInputLayout>

Password field:

<com.google.android.material.textfield.TextInputLayout

    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/passwordWrapper"
    style="@style/MessageError"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal"
    android:textAlignment="center"
    android:layout_marginTop="0dp"
    app:passwordToggleDrawable="@drawable/ic_password_visibility_selector"
    app:passwordToggleEnabled="true"
    app:passwordToggleTint="@color/grey"
    >
<EditText
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:id="@+id/password_edit_text"
    android:layout_below="@+id/login"
    android:layout_marginTop="-10dp"
    android:hint="mot de passe"
    android:inputType="numberPassword"
    android:drawableEnd="@drawable/ic_eye_password"
    android:background="@drawable/custom_input_v3"
    android:padding="10dp"
    android:paddingStart="15dp"
    />
</com.google.android.material.textfield.TextInputLayout>

this is the treatement in java class for adding dot after character number 5

    class myTextWatcher implements TextWatcher {
        int first = 0;
        int second;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

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

        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void afterTextChanged(Editable editable) {
            second = first;
            first = editable.length();
            if(TextUtils.isEmpty(editable.toString())){
                clean.setVisibility(View.GONE);
            }else{
                clean.setVisibility(View.VISIBLE);
            }
            if(editable.length() == 5 && first>second){
                user_edit_text.append(".");
            }
            if(editable.length() == 5 && second>first){
                String newString = editable.toString();
                user_edit_text.setText(newString.substring(0,newString.length()-1));
            }
        }
    }

Try using this, instead of wraping LinearLayout inside your TextInputLayout

<com.google.android.material.textfield.TextInputLayout
  android:id="@+id/userWrapper"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:layout_centerVertical="true"
  android:layout_marginTop="2dp"
  android:textAlignment="center"
  android:layout_marginBottom="5dp"
  android:drawablePadding="10dp"
  android:hint="@string/user_edit_text"
  app:startIconDrawable="@drawable/ic_baseline_cancel_24"
  app:startIconTint="@color/white_with_alpha">

  <com.google.android.material.textfield.TextInputEditText
    android:id="@+id/user_edit_text"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:inputType="datetime"
    android:padding="10dp"
    android:paddingStart="20dp" />
</com.google.android.material.textfield.TextInputLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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