这是 Android Framework 的一个错误,适用于 Android 4.4+ 中的 EditText 字段:
https://issuetracker.google.com/issues/37082815 或
https://code.google.com/p/android/issues/detail?id=201471。截至 2016 年 7 月,该问题尚未解决。
然而,有一种解决方法:
- 要使提示在右侧正确显示(从右到左/RTL 模式),必须在没有输入文本时
删除 InputType 属性
textPassword
(
InputType.TYPE_TEXT_VARIATION_PASSWORD
)。
- 要保留密码输入字段的行为以显示点以隐藏已键入的文本,必须在第一个字符输入后
动态启用 InputType.TYPE_TEXT_VARIATION_PASSWORD
。并在删除所有字符时重置它。
- 为了防止 Latin 字符输入(LTR 文本,如“abc123”)跳到左侧或完全消失的 UI 故障,必须
明确设置 textDirection 为 RTL。
以下是详细信息:
AndroidManifest.xml
的先决条件:
<application
...
android:supportsRtl="true"
... >
</application>
您的XML布局包含:
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:hint="סיסמא"
... />
带有解决Bug的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);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isRTL(getActivity())) {
password.setTextDirection(View.TEXT_DIRECTION_RTL);
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) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isRTL(getActivity())) {
if (s.length() > 0) {
if (!inputTypeChanged) {
password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD |
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
password.setSelection(s.length());
inputTypeChanged = true;
}
} else {
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;
} else {
return false;
}
}
应该像这样工作:
![视频截图,右侧提示,右侧输入密码](https://istack.dev59.com/zYR7y.gif)