programmatically set edit text hint in android?

2019-01-26 02:52发布

问题:

is there a way to set the text hint displayed inside of an android edittext with java? I would like for my app to be able to display different things in the edittext at different times, thanks in advance

回答1:

Did you try the setHint?

myTextView.setHint("My conditional hint");

Hope it helps..



回答2:

If you are using simple Editext without TextInputLayout :

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

If you are using Editext within TextInputLayout then you need to change property of TextInputLayout not Edittext:

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


回答3:

Call setHint() on the EditText.



回答4:

((TextView) findViewById(R.id.yourEditTextId)).setHint("hint text");


回答5:

if you are using actionbarsherlock searchview, this is the best way to set string dynamically

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

searchTextView.setHint(getResources().getString(R.string.search));


回答6:

Kotlin

editText.hint = "Your hint"


回答7:

for editText.hint you must delete name of your edit.Text from activity_main.xml then set in the MainActivity in java or activity_main.xml because text is preferred to hint.



回答8:

You can try following,

Throw an android:hint="hint text" into your EditText xml tag.