EditText clears text at first focus - Android

I have several EditText objects with text inside. I want the first time EditText gets focus to remove the text in it, but only the first time.

How can i do this?

Here is an example: I have an EditText called SomeThing with the text "someText" in it. when the user touches SomeThing for the first time, I want "someText" to be deleted. so let's say that the text was deleted, and now the user typed his own text, this time “someOtherText”, and EditText lost focus for another EditText. This time, when the user touches SomeThing, "someOtherText" will not be deleted, because the second time he receives focus.

+8
java android android-edittext
source share
2 answers

Matan, I'm not sure if this is what you are looking at, but I think you want to display a 'hint' for your Edit Text

Example

<EditText . . android:hint="Please enter your name here"> 

As an example, check out this http://www.giantflyingsaucer.com/blog/wp-content/uploads/2010/08/android-edittext-example-3a.jpg

+9
source share

If you are looking for a way to add a placeholder for EditText , simply add android:hint = 'some text' to the corresponding XML file or call the setHint('some text') method on EditText .

Otherwise, you can use OnFocusChangeListener() to respond to the event with the whole focus. To test EditText focus, use another Boolean variable (for example, isFirstTimeGetFocused ) and initialize it to true in the onCreate() method. After focusing the EditText set isFirstTimeGetFocused to false ;

 editText.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus && isFirstTimeGetFocused){ editText.setText(""); isFirstTimeGetFocused = false; } }); 
+2
source share

All Articles