Android add placeholder text to EditText

后端 未结 8 834
感动是毒
感动是毒 2020-12-02 04:34

How can I add a placeholder text to EditText in the class that isn\'t in the XML?

I have the following EditText in my code which will be sh

相关标签:
8条回答
  • 2020-12-02 04:49

    This how to make input password that has hint which not converted to * !!.

    On XML :

    android:inputType="textPassword"
    android:gravity="center"
    android:ellipsize="start"
    android:hint="Input Password !."
    

    thanks to : mango and rjrjr for the insight :D.

    0 讨论(0)
  • 2020-12-02 04:53

    android:hint="text" provides an info for user that what he need to fill in particular editText

    for example :- i have two edittext one for numeric value and other for string value . we can set a hint for user so he can understand that what value he needs to give

    android:hint="Please enter phone number"
    android:hint="Enter name" 
    

    after running app these two edittext will show the entered hint ,after click on edit text it goes and user can enter what he want (see luxurymode image)

    0 讨论(0)
  • 2020-12-02 04:56

    You have to use the android:hint attribute

    <EditText
    android:id="@+id/message"
    android:hint="<<Your placeholder>>"
    />
    

    In Android Studio, you can switch from XML -> Design View and click on the Component in the layout, the EditText field in this case. This will show all the applicable attributes for that GUI component. This will be handy when you don't know about all the attributes that are there.

    You would be surprised to see that EditText has more than 140 attributes for customization.

    0 讨论(0)
  • 2020-12-02 04:59

    Ah, ok. What you're looking for is setHint(int). Simply pass in a resource id of a string from your xml and you're good to go.

    enter image description here

    EDIT

    And in XML, it's simply android:hint="someText"

    0 讨论(0)
  • 2020-12-02 05:01

    If you mean the location where you will add it in the layout. You can define a container like a FrameLayout and add this EditText to it when it is created.

    <LinearLayout xmlns=".."/>
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/container" android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
    
    FrameLayout layout = (FrameLayout) findViewById(R.id.container);
    layout.addView(name);
    
    0 讨论(0)
  • 2020-12-02 05:02

    In your Activity

    <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp"
                    android:background="@null"
                    android:hint="Text Example"
                    android:padding="5dp"
                    android:singleLine="true"
                    android:id="@+id/name"
                    android:textColor="@color/magenta"/>
    

    0 讨论(0)
提交回复
热议问题