How do I position the cursor on the right in EditT

2019-02-06 00:08发布

问题:

My application uses RTL language (right to left).

When the EditText field gets focus, the cursor appears on the left and only when the user starts to type, the cursor (and the RTL text) moves right. When the user clicks "enter" to start a new line, the cursor moves to the left again.

When I use android:gravity="right", the cursor is OK (on the right) but as the user starts to type the text always moves to the the other side (RTL text moves left).

Any ideas how I can align text to the right AND keep the text direction?

回答1:

Its works for me

      <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:ellipsize="end"
            android:gravity="right" />

ellipsize is important

It solves your problem



回答2:

You might want to look into the native RTL support that was introduced in Android 4.2 Jelly Bean.

http://developer.android.com/about/versions/jelly-bean.html

Android 4.2 introduces full native support for RTL (right-to-left) layouts, including layout mirroring. With native RTL support, you can deliver the same great app experience to all of your users, whether their language uses a script that reads right-to-left or one that reads left-to-right.



回答3:

I have the same problem and I am able to fix this by setting android:textDirection in my EditText. e.g

android:textDirection="inherit"

you can change the value of textDirection as per your requirement. I have taken it as inherit because user are able to change the language with in app. So if user has selected language like english or hindi then it will start typing from left-to-right direction, but if user has selected language like urdu then it will start typing right-to-left direction.

For this your app minimum API level should be 17.



回答4:

Starting from API Level>=17

We can use android:layoutDirection in edit text. Also android:gravity="end" should also works.



回答5:

IMHO, android:gravity = right only makes the text jusified to the right. But what you want cannot be done with this. You can call a ontouch event in the textbox and add the new character to the right of the current text and display it again.....