How to set Default Cursor position to right of EditText

一个人想着一个人 提交于 2020-01-06 12:41:27

问题


I have implemented an EditText where I wanted text to start from it's right, and I achieved by set

gravity = right

But the default cursor still shows up at the left of my text.

This is what I have tried so far :

and style sheet code for EditText Field.

<style name="_style_user_profile_editText" parent="@android:style/Widget.EditText">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:textCursorDrawable">@drawable/cursor_color_blue</item>
        <item name="android:inputType">textNoSuggestions</item>
        <item name="android:gravity">right</item>
        <item name="android:singleLine">true</item>
        <item name="android:hint">e.g Joe jr.</item>
        <item name="android:background">@android:color/transparent</item>        
    </style>

I tried with <item name="android:ellipsize">end</item> but result remain same.. any other suggestion to keep cursor position at right?


回答1:


Consider this workaround:

Use another TextView "hintView" to show the hint text "e.g Joe jr.". And add a TextWatcher to your EditText:

    et.addTextChangedListener(new TextWatcher() {

        @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) {
            // Change the hintView's visibility.
            hintView.setVisibility(TextUtils.isEmpty(s) ? View.VISIBLE : View.GONE);
        }
    });


来源:https://stackoverflow.com/questions/26292960/how-to-set-default-cursor-position-to-right-of-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!