Android將佔位符文本添加到EditText

本文翻譯自:Android add placeholder text to EditText

How can I add a placeholder text to EditText in the class that isn't in the XML? 如何在XML以外的類中向EditText添加佔位符文本?

I have the following EditText in my code which will be shown in alertdialog: 我的代碼中包含以下EditText ,這些內容將顯示在alertdialog中:

    final EditText name = new EditText(this);

#1樓

參考:https://stackoom.com/question/YUfw/Android將佔位符文本添加到EditText


#2樓

android:hint="text" provides an info for user that what he need to fill in particular editText android:hint="text"爲用戶提供他需要填寫特定editText

for example :- i have two edittext one for numeric value and other for string value . 例如:-我有兩個edittext,一個用於數字值,另一個用於字符串值。 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) 在運行應用程序之後,這兩個edittext將顯示輸入的提示,單擊edit文本後,用戶可以輸入所需內容(請參見Luxurymode圖片)


#3樓

This how to make input password that has hint which not converted to * !!. 這樣如何使輸入的密碼具有未轉換爲* !!的提示。

On XML : 在XML上:

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

thanks to : mango and rjrjr for the insight :D. 感謝:mango和rjrjr的見解:D。


#4樓

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"/>

在此處輸入圖片說明


#5樓

In Android Studio you can add Hint (Place holder) through GUI. 在Android Studio中,您可以通過GUI添加提示(佔位符)。 First select EditText field on designer view. 首先在設計器視圖上選擇EditText字段。 Then Click on Component Tree Left side of IDE (Normally it's there, but it may be there minimized) There you can see Properties of selected EditText. 然後單擊IDE左側的組件樹 (通常在其中,但可能已最小化)。在這裏,您可以看到所選EditText的屬性 Find Hint field as below Image 找到提示字段,如下圖

在此處輸入圖片說明

There you can add Hint(Place holder) to EditText 在那裏您可以將提示(佔位符)添加到EditText


#6樓

If you want to insert text inside your EditText view that stays there after the field is selected (unlike how hint behaves), do this: 如果要在EditText視圖中插入文本,並在選定字段後將文本保留在其中(與提示的行爲不同),請執行以下操作:

In Java: 在Java中:

// Cast Your EditText as a TextView
((TextView) findViewById(R.id.email)).setText("your Text")

In Kotlin: 在科特林:

// Cast your EditText into a TextView
// Like this
(findViewById(R.id.email) as TextView).text = "Your Text"
// Or simply like this
findViewById<TextView>(R.id.email).text = "Your Text"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章