3

我正在开发一个应用程序,我需要在其中拖动一个文本视图,并在单击它时显示一个对话框。

我在下面添加了我的代码,当我使用 only 时ACTION_MOVE,我可以拖动文本。但是,当我TextDialog.setVisibility(View.VISIBLE); 让我的对话框可见时,我无法拖动文本。这两个事件没有同时进行。

我该如何处理这两个事件?

tvText=  (TextView)findViewById(R.id.text);
    TextDialog=(LinearLayout)findViewById(R.id.Textdialog);
    tvText.setOnTouchListener(this);
    tvText.setOnClickListener(this);
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    layoutParams = (LayoutParams) tvText.getLayoutParams();
    switch(event.getAction())
    {
    case MotionEvent.ACTION_DOWN:  
                        TextDialog.setVisibility(View.VISIBLE);
                                    break;
    case MotionEvent.ACTION_MOVE:
                                    int x = (int)event.getRawX();
                                    int y= (int)event.getRawY();



                                    layoutParams.leftMargin = x - 150;
                                    layoutParams.topMargin = y - 210;


                                    tvText.setLayoutParams(layoutParams);
                                    break;

          default:
                                    break;
    }
          return true;



}
4

2 回答 2

0

也实现 OnClickListener。检查这个

myView.setOnTouchListener(myListener);
myView.setOnClickListener(myListener);

/* MyListener class */
class MyListener implements View.OnTouchListerner, View.OnClickListener {
public void onTouch(View v, MotionEvent e) {
    if (e.ACTION_MOVE) {
        Log.d("ACTION MOVE",""); // now it is called
    } else if (e.ACTION_DOWN) {
        Log.d("ACTION_DOWN"); // called
    }
}

    public void onClick(View v) {
    }
}
于 2013-01-31T09:31:58.410 回答
0

使用任何小部件或布局拖放并将其放入 onTouch()

@Override
    public boolean onTouch(View view1, MotionEvent event) {

        final int X = (int) event.getRawX() - minusDisplayVal;
        final int Y = (int) event.getRawY() - minusDisplayVal;

        LinearLayout.LayoutParams layoutParamsSub = (LinearLayout.LayoutParams) view1
                .getLayoutParams();
        int _xDelta = 0;
        int _yDelta = 0;

        switch (event.getActionMasked() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            _xDelta = X - layoutParamsSub.leftMargin;
            _yDelta = Y - layoutParamsSub.topMargin;
            break;

        case MotionEvent.ACTION_UP:
            _xDelta = X - layoutParamsSub.leftMargin;
            _yDelta = Y - layoutParamsSub.topMargin;
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            _xDelta = X - layoutParamsSub.leftMargin;
            _yDelta = Y - layoutParamsSub.topMargin;
            break;

        case MotionEvent.ACTION_POINTER_UP:
            _xDelta = X - layoutParamsSub.leftMargin;
            _yDelta = Y - layoutParamsSub.topMargin;
            break;

        case MotionEvent.ACTION_MOVE:

            layoutParamsSub.leftMargin = X - _xDelta;
            layoutParamsSub.topMargin = Y - _yDelta;

            if (layoutParamsSub.leftMargin < 0)// //for left
                layoutParamsSub.leftMargin = 0;

            if (layoutParamsSub.topMargin < 0)// ///for top
                layoutParamsSub.topMargin = 0;

            dataTv.setLayoutParams(layoutParamsSub);
            break;
        }
        backgroudColor.invalidate();
        return true;
    }
于 2017-01-04T09:39:21.070 回答