문제

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