Hint Alignment to the right of password EditText

前端 未结 8 1519
耶瑟儿~
耶瑟儿~ 2021-01-17 17:54

I\'m working on and activity with arabic language. I want the hint of the username and password to start from the right and I have no problem f typing started from the left

8条回答
  •  自闭症患者
    2021-01-17 18:52

    Here my solution (workaround) for editText with type = password. Work on Android 5, Android 6.

    edittext_password = (EditText) rootView.findViewById(R.id.edittext_password);
                if (RTLUtils.isLeftToRightLanguage()) {
                    edittext_password.setGravity(Gravity.START);
                } else {
                    // Force a right-aligned text entry, otherwise latin character input,
                    // like "abc123", will jump to the left and may even disappear!
                    edittext_password.setTextDirection(View.TEXT_DIRECTION_RTL);
                    // Make the hint display on the right hand side
                    edittext_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                }
                edittext_password.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                        isLeftToRight = RTLUtils.isLeftToRightLanguage(charSequence.toString());
                    }
    
                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    }
    
                    @Override
                    public void afterTextChanged(Editable editable) {
                        if (editable.length() > 0) {
                            isLeftToRight = RTLUtils.isLeftToRightLanguage(editable.toString());
                            if (isLeftToRight) {
                                edittext_password.setGravity(Gravity.START);
                                edittext_password.setTextDirection(View.TEXT_DIRECTION_LTR);
                                edittext_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                                // Move the cursor to the correct place (after the typed character)
                                edittext_password.setSelection(editable.length());
                            } else {
                                //edittext_password.setGravity(Gravity.END);
                                edittext_password.setTextDirection(View.TEXT_DIRECTION_RTL);
                                // When a character is typed, dynamically change the EditText's
                                // InputType to PASSWORD, to show the dots and conceal the typed characters.
                                edittext_password.setInputType(InputType.TYPE_CLASS_TEXT |
                                        InputType.TYPE_TEXT_VARIATION_PASSWORD |
                                        InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                                // Move the cursor to the correct place (after the typed character)
                                edittext_password.setSelection(editable.length());
                            }
                        } else { // empty text
                            if (!RTLUtils.isLeftToRightLanguage()) {
                                // Must be in this order (first setInputType then setTextDirection)
                                edittext_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                                edittext_password.setTextDirection(View.TEXT_DIRECTION_RTL);
                            }
                        }
                    }
                });
    
      public static boolean isLeftToRightLanguage() {
            Bidi bidi = new Bidi(Locale.getDefault().getDisplayLanguage(), Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
            if (bidi.isLeftToRight()) {
                return true;
            } else {
                return false;
            }
        }
    
        public static boolean isLeftToRightLanguage(String text) {
            Bidi bidi = new Bidi(text, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
            if (bidi.isLeftToRight()) {
                return true;
            } else {
                return false;
            }
        }
    

    And here result: (Arabic soft keyboard)

    and English soft keyboard:

提交回复
热议问题