Вопрос

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

Это было полезно?

Решение

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

Другие советы

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 ..

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top