Horizontal text scroll inside EditText

岁酱吖の 提交于 2020-01-22 15:59:08

问题


I am making an EditText field with custom background, but the input text is going to be longer then the field itself. Also, text needs to be in one line.

My idea is to make text "move to the left" when the line reach the end of the field. I saw in some applications that this is possible.

I searched for answers but non of them respond to this specific problem. After I combined few of them I tried to solve problem in this way:

<EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text"
        android:textColor="#FFFFFF"
        android:paddingLeft="5dp"
        android:maxLines="1"
        android:maxLength="25"
        android:scrollHorizontally="true"/>

But, when I type text and It reaches the end of EditText field, it starts to stretch my field ("@drawable/edit_text" in particular) until I reach 25 characters.

Also, when I change layout_width to some specific value, text goes in 2nd row when reach end (even if maxLines is still 1).


回答1:


After some research, I found it working.

Using android:inputType="text" inside EditText worked for me.

Hope this will help someone and will save your time :)




回答2:


I got it. The point is that you need to put EditText inside RelativeLayout. Now It's working.

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text">
        <EditText
            android:layout_width="120dp"
            android:layout_height="21dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_margin="6dp"
            android:background="#00000000"
            android:imeOptions="actionDone"
            android:inputType="textPersonName"
            android:maxLength="20"
            android:maxLines="1"
            android:paddingLeft="2dp"
            android:textColor="#FFFFFF"/>
    </RelativeLayout>



回答3:


It worked after adding

android:scrollHorizontally="true"
android:singleLine="true"
android:gravity="right|center_vertical" 

In

<EditText
        android:background="@drawable/sk"
        android:id="@+id/ed"
        android:textSize="@dimen/sed"
        android:textColor="@color/tc"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:gravity="right|center_vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

to my edittext




回答4:


Put your EditText inside the HorizontalScrollView and it works..

            <HorizontalScrollView
                android:id="@+id/rlEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:scrollHorizontally="true"
                android:layout_alignLeft="@+id/etPhone"
                android:layout_alignRight="@+id/imgBtnEdit"
                android:layout_below="@+id/etPhone" >
                <EditText
                    android:id="@+id/etEmail"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:ems="10"
                    android:enabled="false"
                    android:focusable="false"
                    android:text="info@friedman.com"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:scrollHorizontally="true" />
            </HorizontalScrollView>


来源:https://stackoverflow.com/questions/17444398/horizontal-text-scroll-inside-edittext

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