问题
I have an EditText set to gravity Right, so that the text starts from the right if the language is Arabic.
Note: My application supports RTL, and I am not setting the TextDirection for my EditText as that will have the same problem. Gravity set to Right does the job perfectly. I have an issue only if I set the InputType to Number or Phone.
If the InputType is set to number/phone there are double cursor at the beginning and end of the text and it is a bit confusing.
To demonstrate this, I have two EditText with InputType Text and Number, Gravity set to Right for both. My application supports RTL and My phone is now set to Arabic language
Manifest
android:supportsRtl="true"
XML
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="text"
android:inputType="text"
android:lines="1"
android:maxLines="1"
android:gravity="right"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/phone"
android:inputType="number"
android:lines="1"
android:maxLines="1"
android:gravity="right"
/>
Here is a screen shot of the behaviour for the second EditText with InputType Number.
Any pointers on how to get rid of the double cursor? or any alternative.
Thanks R
回答1:
The Edit text doesn't know if the next char you will press is a number or a letter so in case of a letter it places the far left one while the far right one for a number(Most likely you know that just making sure).
So it will disappear if you switch to English.
I think this is fundamental in the edit text:
Solutions:
1- You can create your own edit text from scratch(From View).(May take month with all the problems that may arise).
2- Find a way to override the edit text to remove the split.
3- Find a git where someone had solved that problem(which I doubt).
This doesn't seem like a problem as a programmer or a user. A designer maybe will be a bit annoyed but ignoring it is the fastest way.
Sorry if this wasn't usefull.
回答2:
Use properties layout_direction as 'ltr' and gravity 'right'.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/phone"
android:inputType="number"
android:lines="1"
android:maxLines="1"
android:gravity="right" />
This solved my problem.
回答3:
ViewCompat.setLayoutDirection(findViewById(R.id.myedittext), ViewCompat.LAYOUT_DIRECTION_LTR);
it worked for me.
来源:https://stackoverflow.com/questions/48497850/double-cursor-in-edittext-for-input-type-number-phone-rtl-arabic