我在用阿拉伯语工作和活动。我希望用户名和密码的提示从右边开始,从左边开始输入没有问题,但是在我的ui中,我希望提示是右边的。但是当我为edittext添加inputtype时,提示移到左边,我试图用编程的方法解决它,但是没有成功。
爪哇
EditText password = (EditText) findViewById(R.id.input_password);
password.setTypeface(Typeface.DEFAULT);
XML
<EditText
android:id="@+id/input_password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="كلمة المرور"
android:textColorHint="#FFFFFF"
android:inputType="textPassword"
android:background="@null"
android:textColor="#FFFFFF"
android:textSize="20dp"/>
最佳答案
这是android框架中的一个bug,用于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
。并且必须在删除所有字符时重置。
为了防止拉丁字符输入(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;
}
}
它应该这样工作: