Android :: OnTouchListener && OnClickListener combination problem

Description of the problem:

I have TextViewon RelativeLayout, and I want to color it red when the user touches it, and go to another page when it clicks on it. Therefore, I tried to set OnClickListenerto perform the click and OnTouchListenerto implement the touch function ( MotionEvent.ACTION_DOWN), but this combination does not work, because it OnTouchListenermakes it OnClickListenernon-functional (don’t know why).

On the forums, people say that we can implement OnClickusing OnTouch MotionEvent.ACTION_UP, but this one can be launched from my layout TextView(TextView will be clicked if you click it and pull your finger to release it), and this is not the desired behavior, because I want to:
   click = click + release in TextView.

Can someone give me a solution for this, please?

+5
source share
3 answers

you can call View.performClick () when action_up. Hope this helps.

your_txtView.setOnClickListener(new TextView.OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    your_txtView.setOnTouchListener(new TextView.OnTouchListener(){
            @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (MotionEvent.ACTION_DOWN == event.getAction()) {

        } else if (MotionEvent.ACTION_UP == event.getAction()) {
            v.performClick();
        }

        return true;
    }
    });
+21
source

Adel, first click problem, or don’t you get a click at all?

, , . , , click, .

private class CustomTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        TextView tv = (TextView) v.findViewById(R.id.single_line_text);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            tv.setTextColor(COLOR_WHEN_PRESSED);
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            tv.setTextColor(COLOR_WHEN_RELEASED);
            // Action of click goes here
        } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
            tv.setTextColor(COLOR_WHEN_RELEASED);
                            // To handle release outside the layout region
        }
        return false;
    }
}

, .

android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"

, !!!

EDIT: , DOWN, UP. DOWN , UP. , , .

+3

There was the same problem. Solved it by returning false from ACTION_MOVE. I fought with him for several hours, trying different things, but it seems that I continued to ignore this little question ... And now it makes sense. When you return true from onTouch, further processing stops, so that OnClickListener is not aware of any movements and starts onClick even after the pointer moves outside the view.

+2
source

All Articles