Don't do this manually. Android already provides this functionality: "hint text".
OnTouchEvent does not work on Edittext field, is there an alternative?
-
10-12-2021 - |
Frage
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
Lösung
Andere Tipps
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 ..
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow