安卓:获取视图只在其上发布的触摸只在、视图、其上

2023-09-06 10:47:23 作者:原谅我放荡不羁爱自由

我在一个棘手的情况,希望你能帮助我吗。我有几点看法(TextViews),水平线性布局放置了一个又一个。当我美元textview1 p $ PSS,拖我的手指到任何其他的TextView和释放触摸,我希望能够得到其上的手指被取消视图(TextView的)。

I am in a tricky situation, hope you can help me with it. I have few views (TextViews), horizontally placed one after another in a linear layout. When I press on textview1, drag my finger to any other textview and release touch, I want to be able to get the view(textview) on which the finger was lifted.

我走了过去TouchListener API,它说,每一个事件一个事件ACTION_DOWN操作开始。由于其他textviews将不会触发该事件,我怎么可以得到参考上我抬起手指textViews?我试了一下就算了,在action_up将在引发该事件ACTION_DOWN TextView的只有火。

I went over the TouchListener api, it says that every event starts with a ACTION_DOWN event action. Since other textviews won't fire that event, how can I get the reference to the textViews on which I lifted my finger? I tried it even, and the action_up would fire only on the textView that fired the action_down event.

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_UP:
        Log.i(TAG, "ACTION_UP");
        Log.i(TAG, ((TextView) v).getText().toString());
        break;

    case MotionEvent.ACTION_DOWN:
     Log.i(TAG, "ACTION_DOWN");
     Log.i(TAG, ((TextView)v).getText().toString());
     break;
    }

    return true;
}

任何帮助是极大AP preciated。谢谢

Any help is greatly appreciated. Thank you

推荐答案

您需要处理中的LinearLayout所有的触摸事件,并检查子视图(child.getLeft()和child.getRight())的位置。

You need to handle all your touch events in the LinearLayout and check the location of the child views (child.getLeft() and child.getRight()).

public boolean dispatchTouchEvent(MotionEvent event){
    int x = event.getX();
    int cc = getChildCount();
    for(int i = 0; i < cc; ++i){
        View c = getChildView();
        if(x > c.getLeft() && x < c.getRight()){
            return c.onTouchEvent(event);
        }
    }
    return false;
}