2016-11-22 65 views
0

我有4個edittext,輸入類型爲numberPassword,每個輸入類型的最大長度爲1.只要輸入一個數字,我將以編程方式將焦點更改爲下一個字段。我希望一旦焦點被改變到下一個字段時,數字就被掩蓋,但是當我在3時,發生了什麼是不可預測的,然後只有1個被掩蓋,或者當我在4時,然後3和2被掩蓋但不是1,有時候他們沒有被掩蓋,但只要我manually集中在任何領域它被掩蓋。當焦點變爲下一個字段時,最後一個字段應該被屏蔽,我也希望有一些一致性。 enter image description hereEditext輸入類型的行爲很奇怪

XML佈局:

<android.support.constraint.ConstraintLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="32dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/textView3" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1"> 

       <EditText 
      android:id="@+id/et_mpin_1" 
      android:layout_width="51dp" 
      android:layout_height="44dp" 
      android:ems="10" 
      android:gravity="center_horizontal" 
      android:inputType="numberPassword" 
      android:maxLength="1" 
      android:maxLines="1" 
      android:imeOptions="actionNext" 
      android:focusableInTouchMode="true" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintTop_toTopOf="parent" 
      tools:layout_constraintLeft_creator="1" 
      tools:layout_constraintTop_creator="1"> 
      <requestFocus /> 
     </EditText> 

     <EditText 
      android:id="@+id/et_mpin_2" 
      android:layout_width="51dp" 
      android:layout_height="0dp" 
      android:layout_marginStart="8dp" 
      android:ems="10" 
      android:gravity="center_horizontal" 
      android:inputType="numberPassword" 
      android:maxLength="1" 
      android:maxLines="1" 
      android:imeOptions="actionNext" 
      app:layout_constraintBottom_toBottomOf="@+id/et_mpin_1" 
      app:layout_constraintLeft_toRightOf="@+id/et_mpin_1" 
      app:layout_constraintTop_toTopOf="@+id/et_mpin_1" 
      app:layout_constraintVertical_bias="0.0" 
      tools:layout_constraintBottom_creator="1" 
      tools:layout_constraintTop_creator="1" /> 

     <EditText 
      android:id="@+id/et_mpin_4" 
      android:layout_width="51dp" 
      android:layout_height="0dp" 
      android:layout_marginStart="8dp" 
      android:ems="10" 
      android:gravity="center_horizontal" 
      android:inputType="numberPassword" 
      android:maxLength="1" 
      android:maxLines="1" 
      android:imeOptions="actionDone" 
      app:layout_constraintBottom_toBottomOf="@+id/et_mpin_1" 
      app:layout_constraintLeft_toRightOf="@+id/et_mpin_3" 
      app:layout_constraintTop_toTopOf="@+id/et_mpin_1" 
      app:layout_constraintVertical_bias="0.0" 
      tools:layout_constraintBottom_creator="1" 
      tools:layout_constraintTop_creator="1" /> 

     <EditText 
      android:id="@+id/et_mpin_3" 
      android:layout_width="51dp" 
      android:layout_height="0dp" 
      android:layout_marginEnd="59dp" 
      android:layout_marginStart="59dp" 
      android:ems="10" 
      android:gravity="center_horizontal" 
      android:inputType="numberPassword" 
      android:maxLength="1" 
      android:maxLines="1" 
      android:imeOptions="actionNext" 
      app:layout_constraintBottom_toBottomOf="@+id/et_mpin_1" 
      app:layout_constraintLeft_toLeftOf="@+id/et_mpin_2" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="@+id/et_mpin_1" 
      app:layout_constraintVertical_bias="1.0" 
      tools:layout_constraintBottom_creator="1" 
      tools:layout_constraintLeft_creator="1" 
      tools:layout_constraintRight_creator="1" 
      tools:layout_constraintTop_creator="1" /> 
    </android.support.constraint.ConstraintLayout> 

Java代碼:

et_mpin1.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
//    et_mpin1.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); 
//    et_mpin1.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       // checking before only because for max lenght 1 start is always going to be 0 
       if (before == 0) { 

        et_mpin2.requestFocus(); 
       } 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
     et_mpin2.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) { 
       // checking before only because for max lenght 1 start is always going to be 0 
       if (before == 0) { 
        et_mpin3.requestFocus(); 
       } else { 
        et_mpin1.requestFocus(); 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
     et_mpin3.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) { 
       // checking before only because for max lenght 1 start is always going to be 0 
       if (before == 0) { 
        et_mpin4.requestFocus(); 
       } else { 
        et_mpin2.requestFocus(); 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 
     et_mpin4.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) { 
       // checking before only because for max lenght 1 start is always going to be 0 
       if (before == 0) { 
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getContext().INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(et_mpin4.getWindowToken(), 0); 
       } else { 
        et_mpin3.requestFocus(); 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 

回答

1

要隱藏密碼有力,

android.provider.Settings.System.putInt(this.getContentResolver(),android.provider.Settings.System.TEXT_SHOW_PASSWORD, 0); 

注意:它不會顯示任何字符,直接點擊將 顯示在您的視圖內沒有任何延遲。

+0

你可以舉一個例子:如何使用edittext – Sunny

+0

@Sunny你不必用'EditText'來做,你只需要在'Activity'中寫入。 – Ironman

+0

@孫尼很高興知道這一點。 – Ironman