-3

I follow this guide to clear edit field when enter Clear text in EditText when entered

But I have to click 2 times for clearing the edit field.

Here is the code:

public class Trackfolio extends Activity implements OnClickListener {
    /**
     * Called when the activity is first created.
     */
    public EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        editText = (EditText) findViewById(R.id.editText1);

        editText.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        editText.setText("");
    }

}

My question is how to clear the edit text in only one click?

I really appreciate your helps. Thank you very much in advanced.

Community
  • 1
  • 1
Lucky Luke
  • 609
  • 1
  • 6
  • 18
  • 3
    Are your sure about your issue ? . The provided code will work with single button click . . . – Don Chakkappan May 27 '15 at 09:35
  • 2
    I don't think your question is valid. Just check your code properly. – M D May 27 '15 at 09:45
  • i really don't believe in down vote, as some question naive to someone is hard for other to get – Syed Raza Mehdi May 27 '15 at 09:48
  • 1
    @MD Actually my problem caused by `OnFocusable`, I must implement `OnFocusChangeListener` instead of `OnClickListener`. I'm really disappointed because you even do not make any help but vote me down. – Lucky Luke Jun 04 '15 at 14:34
  • I had the same problem, thanks for pointing me to OnFocusChangeListener – Gloegg Apr 07 '16 at 18:24

3 Answers3

1

you can use this

editText.getText().clear();
Jai
  • 486
  • 1
  • 8
  • 21
0
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    editText = (EditText) findViewById(R.id.editText1);

    editText.setFocusableInTouchMode(true);
    editText.setFocusable(true);
    editText.requestFocus();
    editText.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.editText1:
         editText.setText("");
        break;

    default:
        break;
    }

  }

}

i think this will solve your problem let me know if it won't

Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47
0

Add two more attributes in your EditText xml focusale & clickable

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/editText"
                android:focusable="true"
                android:clickable="true" />
NarenderNishad
  • 1,058
  • 1
  • 17
  • 31