这是 Android 框架中的一个错误,适用于 Android 4.4+ 中的 EditText 字段:https://issuetracker.google.com/issues/37082815 or https://code.google.com/p/android/issues/detail?id=201471。截至2016年7月,该问题目前尚未解决。
不过,有一种方法可以解决这个问题:
要使提示正确显示在右侧(从右到左/RTL 模式),您必须remove输入类型属性textPassword
(InputType.TYPE_TEXT_VARIATION_PASSWORD
),当没有输入文本时。
要保留显示点以隐藏键入文本的密码输入字段行为,您必须动态启用 InputType.TYPE_TEXT_VARIATION_PASSWORD
,当输入第一个字符时。并且当删除所有字符时必须重置。
为了防止拉丁字符输入(LTR 文本,如“abc123”)的 UI 故障跳到左侧或完全消失,您必须显式将 textDirection 设置为 RTL.
详细信息如下:
您的先决条件AndroidManifest.xml
:
<application
...
android:supportsRtl="true"
... >
</application>
您的 XML 布局包含:
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:hint="סיסמא"
... />
具有解决方法错误修复的 Java 代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_fragment_layout, container, false);
final EditText password = (EditText) view.findViewById(R.id.password);
// Workaround https://issuetracker.google.com/issues/37082815 for Android 4.4+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isRTL(getActivity())) {
// Force a right-aligned text entry, otherwise latin character input,
// like "abc123", will jump to the left and may even disappear!
password.setTextDirection(View.TEXT_DIRECTION_RTL);
// Make the "Enter password" hint display on the right hand side
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
password.addTextChangedListener(new TextWatcher() {
boolean inputTypeChanged;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
// Workaround https://code.google.com/p/android/issues/detail?id=201471 for Android 4.4+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isRTL(getActivity())) {
if (s.length() > 0) {
if (!inputTypeChanged) {
// When a character is typed, dynamically change the EditText's
// InputType to PASSWORD, to show the dots and conceal the typed characters.
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)
password.setSelection(s.length());
inputTypeChanged = true;
}
} else {
// Reset EditText: Make the "Enter password" hint display on the right
password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
inputTypeChanged = false;
}
}
}
});
return view;
}
public static boolean isRTL(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return context.getResources().getConfiguration().getLayoutDirection()
== View.LAYOUT_DIRECTION_RTL;
// Another way:
// Define a boolean resource as "true" in res/values-ldrtl
// and "false" in res/values
// return context.getResources().getBoolean(R.bool.is_right_to_left);
} else {
return false;
}
}
它应该像这样工作: