拖动按钮称为查看问题和解决方法

时间:2018-06-25 02:12:27

标签: java android android-studio drag-and-drop draggable

在这种情况下,大家可以帮助我 我想拖动一个名为view的按钮, 我像在下面看到的那样设置了代码,但是我仍然无法将其拖动,能否以很好的解释为我提供差异解决方案

public class MainActivity extends Activity {

    private Timer _timer = new Timer();

    private LinearLayout linear1;
    private Button view;
    private Button btn2;
    private Button widgets;
    private TextView textview1;

    private TimerTask mm;
    @Override
    protected void onCreate(Bundle _savedInstanceState) {
        super.onCreate(_savedInstanceState);
        setContentView(R.layout.main);
        initialize();
        initializeLogic();
    }

    private void initialize() {

        linear1 = (LinearLayout) findViewById(R.id.linear1);
        view = (Button) findViewById(R.id.view);
        btn2 = (Button) findViewById(R.id.btn2);
        widgets = (Button) findViewById(R.id.widgets);
        textview1 = (TextView) findViewById(R.id.textview1);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setOnDragListener( new View.OnDragListener() {
                    PointF DownPT = new PointF();
                    PointF StartPT = new PointF();
                    @Override public boolean onDrag(View v, android.view.DragEvent event) {
                        int dragEvent = event.getAction();
                        switch (dragEvent) {
                            case android.view.DragEvent.ACTION_DRAG_STARTED :
                            DownPT.x = event.getX();
                            DownPT.y = event.getY();
                            StartPT = new PointF( view.getX(), view.getY() ); break;

                            case android.view.DragEvent.ACTION_DRAG_LOCATION :
                            PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
                            view.setX((int)(StartPT.x+mv.x)); view.setY((int)(StartPT.y+mv.y));
                            StartPT = new PointF( view.getX(), view.getY() ); break;

                            case android.view.DragEvent.ACTION_DRAG_ENTERED :

                            case android.view.DragEvent.ACTION_DRAG_EXITED :
                            break;
                            case android.view.DragEvent.ACTION_DROP : break;
                            default : break;
                        }
                        return true;
                    }
                });

我需要一个很好的说明专家,因为我需要了解问题,谢谢您抽出宝贵时间来帮助我! 我需要知道有什么不同 解决此问题的解决方案

1 个答案:

答案 0 :(得分:0)

在onClick方法之外实施此操作:-

  view.setOnDragListener( new View.OnDragListener() {
                PointF DownPT = new PointF();
                PointF StartPT = new PointF();
                @Override public boolean onDrag(View v, android.view.DragEvent event) {
                    int dragEvent = event.getAction();
                    switch (dragEvent) {
                        case android.view.DragEvent.ACTION_DRAG_STARTED :
                        DownPT.x = event.getX();
                        DownPT.y = event.getY();
                        StartPT = new PointF( view.getX(), view.getY() ); break;

                        case android.view.DragEvent.ACTION_DRAG_LOCATION :
                        PointF mv = new PointF( event.getX() - DownPT.x, event.getY() - DownPT.y);
                        view.setX((int)(StartPT.x+mv.x)); view.setY((int)(StartPT.y+mv.y));
                        StartPT = new PointF( view.getX(), view.getY() ); break;

                        case android.view.DragEvent.ACTION_DRAG_ENTERED :

                        case android.view.DragEvent.ACTION_DRAG_EXITED :
                        break;
                        case android.view.DragEvent.ACTION_DROP : break;
                        default : break;
                    }
                    return true;
                }
            });

不在onClick()内。希望这会有所帮助。