Hint and Text in editText at same time

你离开我真会死。 提交于 2021-02-19 01:53:25

问题


I just need an editText in which i can set Text and hint at the same time. e.g i want user to enter in editText string like user#1234 in this string user# should be text after that numbers will vary so that 1234 should be show as hint when user try to enter cursor will go after # and 1234 hint will gone when user type a character.

Please guide me how to do this.

Thanks


回答1:


I managed to achieved this with bit of tricks and playing with margin and padding

This is what I did

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >

<TextView
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:gravity="right"
    android:text="user#" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="1234"
    android:paddingLeft="40dp"
    android:textSize="14sp" />

</RelativeLayout>

Hope this will help you out




回答2:


This is not possible in the Android's default EditText but if want to achieve this any how, you will have to work a bit.

You can use this library called Masked-EditText. Read the documentation and understand how you can use it to solve your purpose. Hope that helps.

[EDIT]

I've got one more trick that you can definitely use. Implement an onChangeListener on the edit text. Every time it is called, run a function. The function should get the text in the editText and get the substring (first 5 char). If the substring matches "user#" then do not do anything. Else replace the text in editText with "user#". Easy hah!




回答3:


You can do this in 2 ways

#1

Place an Image with user# as drawableLeft to your EditText

#2

Use a RelativeLayout to place a TextView and EditText one above the other so that it shows text in TextView as hint in EditText as below :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="user#"
    android:textColor="#aaaaaaaa"
    android:textSize="30dip" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView1"
    android:layout_alignBottom="@+id/textView1"
    android:layout_toRightOf="@+id/textView1"
    android:background="#00000000"
    android:textSize="30dip">

</EditText>



来源:https://stackoverflow.com/questions/36153633/hint-and-text-in-edittext-at-same-time

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