简体   繁体   中英

Custom EditText in Android with separated bottom lines

I need to make EditText with separated bottom lines looks like in the image below EditText

I tried to put EditText and four ImageView into RelativeLayout.

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:gravity="center">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:gravity="center"
                    android:orientation="horizontal">


                    <EditText
                        android:id="@+id/et_code"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@null"                                   
                        android:singleLine="true"
                        android:textColor="@color/black"
                        android:textSize="16dp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="1px"

                        android:alpha="0.3"
                        android:background="@color/black" />

                    <ImageView
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="1px"
                        android:alpha="0.3"
                        android:background="@color/black" />

                    <ImageView
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="1px"
                        android:alpha="0.3"
                        android:background="@color/black" />

                    <ImageView
                        android:layout_weight="1"
                        android:layout_width="wrap_content"
                        android:layout_height="1px"
                        android:alpha="0.3"
                        android:background="@color/black" />

                </LinearLayout>

            </RelativeLayout>
        </RelativeLayout>

This is very bad solution because in small screens numbers begin to move out.

Use this library.

<com.github.glomadrian.codeinputlib.CodeInput
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:underline_color="#457ad1"
app:underline_selected_color="#9e1ace"
app:text_color="#b12eff"
app:hint_color="#77ce9d"
app:hint_text="Pin code"
app:codes="4"
/>

Add in dependencies

compile 'com.github.glomadrian:CodeInput:1.1@aar'

For this you require to add four edittext each with width = "0dp" and weight = "1" inside linearlayout with horizontal orientation. Something like this

<LinearLayout
     android:layout_width="match_parent"
     android:layout_height = "wrap_content"
     android:orientation="horizontal"
>
   <Edittext 
     android:layout_width="0dp"
     android:layout_height = "wrap_content"
     android:layout_weight = "1"
     .... add other required attributes
   />
<Edittext 
     android:layout_width="0dp"
     android:layout_height = "wrap_content"
     android:layout_weight = "1"
     .... add other required attributes
   />
<Edittext 
     android:layout_width="0dp"
     android:layout_height = "wrap_content"
     android:layout_weight = "1"
     .... add other required attributes
   />
<Edittext 
     android:layout_width="0dp"
     android:layout_height = "wrap_content"
     android:layout_weight = "1"
     .... add other required attributes
   />
</LinearLayout>

This is only for illustration you will require to implement it your way with your required style and attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM