i'm looking a way to put text on edittext ontouch event, but set edittext text must just when first click after activity launch, after that if we touch edittext again, settext will be disable,

i use this code :

       edittext1.setOnTouchListener(new View.OnTouchListener(){
        public boolean onTouch(View view, MotionEvent motionEvent) {                                                       
             // your code here....
            edittext1.setText("Is this have two or tree ?");
             return false;
        }
    });

and yes settext always set "Is this have two or tree ?" , even i change the text of edittext, if i touch edittext again it will back to "Is this have two or tree ?".

有帮助吗?

解决方案

If you want to do it on launch:

edittext1.setText("my text");

If you want to do it on first touch only, you will have to preate a varialbe to hold that state:

boolean wasSet = false;

onTouch(){
    if(!wasSet){
        //First time around
        edittext1.setText("my text");
        wasSet = true;
    }else{
        //Second time around
    }
}

Altough, what is is looking like you would rather do it:

edittext1.setHint("mytext");

Which will display an entry hint whenever the EditText is empty of user content.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top