0

I have a edittext field which prefilled with the value "comment please..." Now I want to delete this content as soon as the User touches this field. In the documentation I saw that there is the possibility to add an onTouchEvent to a edittext field. But its not working because compiler error occurs with

The method onTouchEvent(MotionEvent) in the type TextView is not applicable for the arguments (new View.OnTouchListener(){})

My code is:

        comment.onTouchEvent(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (comment.getText().equals( R.string.comment_content )){
                    comment.setText( "" );
                    return true;
                }
                return false;

            }});

thanks

Al Phaba
  • 6,545
  • 12
  • 51
  • 83

2 Answers2

1

Don't do this manually. Android already provides this functionality: "hint text".

kabuko
  • 36,028
  • 10
  • 80
  • 93
0
MyEditor.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
    int inType = MyEditor.getInputType(); // backup the input type
    MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
    MyEditor.onTouchEvent(event); // call native handler
    MyEditor.setInputType(inType); // restore input type
    return true; // consume touch even
}
});

This one is disable your input and set on touch work ..

QuokMoon
  • 4,387
  • 4
  • 26
  • 50