2012-04-17 109 views
4

我希望能夠以填充添加到在的EditText中顯示的文本提示文本,使用填充於內部的EditText

android:hint="some text" 

如果我只爲定期的EditText添加填充,它增加了填充的整個視圖。我希望它能夠提供給用戶提示的文本,需要輸入的內容。如果有人知道如何做到這一點,請給我一個反饋。

enter image description here

回答

0

使用TextChangedListener和TextWatcher監視的EditText中的文本。使用TextWatcher檢查來查看EditText是否爲空並設置填充,否則刪除填充。

+0

對不起,如果我不清楚我想做什麼。我編輯了我的問題並添加了一張圖片。正如你所看到的那樣,給用戶一個提示的文本,應該在edittext字段中輸入的內容不會粘到它的左邊緣。它有一個填充。我不知道是否可以使用TextWatcher來完成。另外,當用戶輸入一些文本時,我希望保持距離(填充)。 – Sandra 2012-04-18 07:48:51

16

您可能會發現這樣的回答您的解決方案: EditText set text start 10dp from left border

它幫助我實現暗示填充在用戶名字段:

<EditText 
android:id="@+id/txtUsername" 
android:layout_width="match_parent" 
android:layout_height="30dp" 
android:layout_alignParentLeft="true" 
android:background="@drawable/bkg_login_txt_usuario" 
android:hint="@string/hint_username" 
android:paddingLeft="10dp" 
android:singleLine="true" > 
<requestFocus /> 
</EditText> 

我希望這有助於。

+1

偉大的答案。謝謝! – CraZyDroiD 2014-09-03 05:41:21

+0

'android:singleLine =「true」'已被棄用。使用'android:maxLines =「1」' – 2017-12-01 08:30:24