2012-02-05 139 views
26

有沒有一種方法來設置文本提示顯示在一個android編輯文本內用java?我想爲我的應用程序能夠在不同的時間在edittext中顯示不同的東西,在此先感謝以編程方式在android中設置編輯文本提示?

+2

在問這些問題之前,只需引用Android開發者一方對於EditText鏈接是在這裏http://developer.android.com/reference/android/widget/EditText.html – 2012-02-06 04:52:26

回答

56

你試過setHint嗎?

myTextView.setHint("My conditional hint"); 

希望它可以幫助..

-3

你可以試試以下,

拋出一個android:hint="hint text"到您的EditText XML標籤。

3

如果你正在使用actionbarsherlock搜索查看,這是設置字符串動態

AutoCompleteTextView searchTextView = 
        (AutoCompleteTextView) mSearchView.findViewById(R.id.abs__search_src_text); 

searchTextView.setHint(getResources().getString(R.string.search)); 
6
((TextView) findViewById(R.id.yourEditTextId)).setHint("hint text"); 
6

如果使用的是簡單的Editext沒有TextInputLayout的最佳方式:

EditText etUsername; 
etUsername = (EditText) findViewById(R.id.etUsername); 
etUsername.setHint("Your Hint"); 

如果您正在使用EditextTextInputLayout之內,那麼你需要更改TextInputLayout的屬性而不是Edittext

TextInputLayout layoutUser; 
layoutUser = (TextInputLayout) findViewById(R.id.inputLayoutUname); 
layoutUser.setHint(getResources().getString(R.string.hintFaculty));