android setOnTouchListener更改多个项目

android setOnTouchListener更改多个项目,android,Android,我有很多文本视图,我想在用户一次单击鼠标就将光标拖动到这些文本视图上时更改所有项目的颜色 我使用了setontouchlistener,但此方法仅为第一个按钮调用操作。当用鼠标滑过鼠标时,在同一次点击中是否还有其他操作 这是我的代码: import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View

我有很多文本视图,我想在用户一次单击鼠标就将光标拖动到这些文本视图上时更改所有项目的颜色

我使用了
setontouchlistener
,但此方法仅为第一个按钮调用操作。当用鼠标滑过鼠标时,在同一次点击中是否还有其他操作

这是我的代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  {

    TextView tv1,tv2,tv3,tv4,tv5;
    Button bu,bu1,bu2;


    MyTouchListener touchListener = new MyTouchListener();

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

        tv1.setId(1);
        tv2.setId(2);
        tv3.setId(3);
        tv4.setId(4);
        tv5.setId(5);


        tv1.setOnTouchListener(touchListener);
        tv2.setOnTouchListener(touchListener);
        tv3.setOnTouchListener(touchListener);
        tv4.setOnTouchListener(touchListener);
        tv5.setOnTouchListener(touchListener);
    }

    public class MyTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(v.getId()){
                case 1:
                    tv1.setTextColor(0xffff0000);
                    break;
                case 2:
                    tv2.setTextColor(0xffff0000);
                    break;
                case 3:
                    tv3.setTextColor(0xffff0000);
                    break;
                case 4:
                    tv4.setTextColor(0xffff0000);
                    break;
                case 5:
                    tv5.setTextColor(0xffff0000);
                    break;
            }
            return true;
        }

    }

}

若我的理解是正确的,那个么当用户触摸屏幕或在触摸屏幕时手指在文本视图的内容上移动时,您希望代码执行。由于默认情况下并非所有触摸事件都传递给子视图,因此处理此问题的一种方法是重写活动的onTouchEvent方法,并对照每个TextView的位置进行检查。我给你举了一个这样的例子:

pulic class SomeActivity extends Activity {
    private TextView tv01;
    ... 
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        // Get the center location of the TextView. 
        int[] tv01pos = {(int) tv01.getX(), (int) tv01.getY()};

        // Some debuging logs, may be deleted.
        Log.d("position TV x", String.valueOf(tv01pos[0]));
        Log.d("position TV y", String.valueOf(tv01pos[1]));

        // Get the width and height of textView 01 for calculating the right and bottom boundaries.
        int tv01Wide =  tv01.getWidth();
        int tv01High = tv01.getHeight();

        // Get the location of the touch event, event may be of type Donw, Move, or Up. 
        // Keep in mind there are a LOT of these generated.
        int[] touchPos = new int[2];
        touchPos[0] = (int) event.getAxisValue(MotionEvent.AXIS_X);
        touchPos[1] = (int) event.getAxisValue(MotionEvent.AXIS_Y);

        // More debug logs.
        Log.d("position Tch x", String.valueOf(touchPos[0]));
        Log.d("position Tch y", String.valueOf(touchPos[1]));

        // Check if the touch event is in the boundaries of TextView 01
        if (    (touchPos[0] > tv01pos[0]           ) &&
                (touchPos[0] < tv01pos[0] + tv01Wide) &&
                (touchPos[1] > tv01pos[1]           ) &&
                (touchPos[1] < tv01pos[1] + tv01High)) {

            // A toast letting you know it's working.  Put your code here.
            Toast.makeText(view.getContext(), "In TextView01", Toast.LENGTH_SHORT).show();
        }

        return true;
    }
}
pulic类SomeActivity扩展活动{
私有文本视图tv01;
... 
@凌驾
公共布尔onTouchEvent(运动事件){
//获取TextView的中心位置。
int[]tv01pos={(int)tv01.getX(),(int)tv01.getY()};
//可能会删除某些调试日志。
Log.d(“位置TV x”,字符串.valueOf(tv01pos[0]);
Log.d(“位置TV y”,字符串值of(tv01pos[1]);
//获取textView 01的宽度和高度,以计算右侧和底部边界。
int tv01Wide=tv01.getWidth();
int tv01High=tv01.getHeight();
//获取触摸事件的位置,事件类型可能为Donw、Move或Up。
//请记住,有很多这样的问题。
int[]touchPos=新int[2];
touchPos[0]=(int)event.getAxisValue(MotionEvent.AXIS_X);
touchPos[1]=(int)event.getAxisValue(MotionEvent.AXIS_Y);
//更多调试日志。
Log.d(“位置Tch x”,字符串.valueOf(touchPos[0]);
Log.d(“位置y”,字符串.valueOf(touchPos[1]);
//检查触摸事件是否在TextView 01的边界内
如果((touchPos[0]>tv01pos[0])&&
(触摸位置[0]tv01pos[1])&&
(触摸位置[1]
发布您已经尝试过的代码。代码只是示例Chuck谢谢Derek您给了我一个开始的好主意我使用了eventmotion.getRawY()和getRawX来Locte touch,您的示例无论如何都需要api 16或更高版本,但是使用了您的想法,经过多次尝试之后就可以使用了。谢谢