简体   繁体   中英

How I can retrieve the id of the item that has been touched?

I have the following Activity:

package com.example.dragtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button selectedButton=null;
    private Button circle=null;
    private Button rectangle=null;
    private Button triangle=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rectangle=(Βutton)findViewById(R.id.rectangle);
        rectangle.setOnTouchListener(new MyTouchListener());
        rectangle.setOnDragListener(new MyDragListener());

        circle=(Βutton)findViewById(R.id.circle);
        circle.setOnTouchListener(new MyTouchListener());
        circle.setOnDragListener(new MyDragListener());

        triangle=(Button)findViewById(R.id.triangle)
        triangle.setOnTouchListener(new com.example.dragtest.MainActivity.MyTouchListener());
        triangle.setOnDragListener(new com.example.dragtest.MainActivity.MyDragListener());

    }

    private final class MyTouchListener implements OnTouchListener {
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                selectedButton
            }
        }
    }

    class MyDragListener implements OnDragListener {
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    // do nothing
                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    v.setBackgroundDrawable(enterShape);
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    v.setBackgroundDrawable(normalShape);
                    break;
                case DragEvent.ACTION_DROP:
                    // Dropped, reassign View to ViewGroup

                    break;
                case DragEvent.ACTION_DRAG_ENDED:

                default:
                    break;
            }
            return true;

        }
    }

}

And I want to detect onMytouch Listened the Item that has been touched in order to retrieve its id. Do you know how I can do that?

You are passing an instance of a class ( MyTouchListener ) instead of an interface ( OnTouchListener ), that means you have the implementation in MyTouchListener . So, either you have to put your implementation inside your listener class:

private final class MyTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            // selectedButton
            final int id = view.getId();
            // Here goes all the works for this view
        }
    }
}

Or, implementation inside the listener interface:

rectangle.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            // selectedButton
            final int id = view.getId();
            // Here goes all the works for this view
        }
    }
});

You can also make an instance of the listener instead of the annonymous one above.

One simply cannot say implements (an interface ) and then forget about the @Override annotation (the other answer features the same consequential error due to copy & paste).

When adding in implements OnTouchListener, OnDragListener , class MainActivity will be underlined in red; just move the mouse there, hit Alt + Enter and it will add the desired implementations, including proper annotations.

In Java this can be written a whole lot more tidy and organized:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener, View.OnDragListener {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);

        rectangle = (Βutton) findViewById(R.id.rectangle);
        rectangle.setOnTouchListener(this);
        rectangle.setOnDragListener(this);

        circle = (Βutton) findViewById(R.id.circle);
        circle.setOnTouchListener(this);
        circle.setOnDragListener(this);

        triangle = (Button) findViewById(R.id.triangle)
        triangle.setOnTouchListener(this);
        triangle.setOnDragListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ...
        };
    }

    @Override
    public boolean onDrag(View view, DragEvent event) {
        int action = event.getAction();
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                // do nothing
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                view.setBackgroundDrawable(enterShape);
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                view.setBackgroundDrawable(normalShape);
                break;
            case DragEvent.ACTION_DROP:
                // Dropped, reassign View to ViewGroup
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                break;
        }
        return true;
    }
}

When dispatching all events to the same listener, one has to tell apart view by their resource ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM