当手指在屏幕上移动时,OnTouchListener中的ACTION_MOVE事件无法正常工作。

3

这是我的要求:

  1. 触摸屏幕。
  2. 保持手指移动。
  3. 如果您在移动手指时触摸屏幕上的任何按钮,则该按钮的颜色会更改为绿色。

问题:第57行(.java)中的“if”条件不起作用。

if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) 直到它变成 if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) ,这不是我想要的。

请查看下面的 .java 文件:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener , GestureDetector.OnGestureListener{

    private Button bt1,bt2,bt3
    
    private GestureDetector mGestureDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        // ------ buttons findViewByID
      
            bt1 = (Button) findViewById(R.id.b1);
            bt2 = (Button) findViewById(R.id.b2);
            bt3 = (Button) findViewById(R.id.b3);  
       

        //------Set UP id for buttons    

            bt1.setId(1);
            bt2.setId(2);
            bt3.setId(3);
        
       //-------set up touchlisteners for buttons
        
            bt1.setOnTouchListener(this);
            bt2.setOnTouchListener(this);
            bt3.setOnTouchListener(this);
    
        //
        mGestureDetector = new GestureDetector(this,this);

    }
    //---------Override Event
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
    
       /*----------When finger  touched + finger is moving. But the  condition below is not working  finger moving event (ACTION_MOVE) not working. but finger touch event(ACTION_DOWN) is working*/
        if (motionEvent.getAction() == MotionEvent.ACTION_MOVE)   // condition doesn't work until it changes                                //if(motionEvent.getAction() == MotionEvent.ACTION_DOWN).
        {
                switch (view.getId())
                    {
                    case 1:
                        bt1.setBackgroundColor(GREEN);
                        break;
                    case 2:
                        bt2.setBackgroundColor(GREEN);
                        break;
                    case 3:
                        bt3.setBackgroundColor(GREEN);
                        break;
                    }
                            
        }

            return true;
                }
//--------Other Overrides
    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {

        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

 
 
 }

请查看下面的 .XML 文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/lin"

        tools:context=".MainActivity">
        
        //------Mode: HORIZONTAL
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            // ------ BUTTONS
        <Button
                android:id="@+id/b1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="60sp"
                android:layout_weight="1"/>
        <Button
                android:id="@+id/b2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="60sp"
                android:layout_weight="1" />
        <Button
                android:id="@+id/b3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="60sp"
                android:layout_weight="1"/>
    </LinearLayout>
    
</LinearLayout>
1个回答

2
这是因为当您在按钮上设置onTouchListener并首次在容器(LinearLayout)上移动时,触摸事件不会传递到子项(按钮)。要使其正常工作,您需要在容器上设置侦听器,然后检查触摸坐标是否包含在按钮的矩形区域内。 代码:
private Button bt1, bt2, bt3;
private LinearLayout lin;
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // ------ buttons findViewByID
    bt1 = (Button) findViewById(R.id.b1);
    bt2 = (Button) findViewById(R.id.b2);
    bt3 = (Button) findViewById(R.id.b3);
    lin = findViewById(R.id.lin);
    //-------set up touchlisteners for buttons
    lin.setOnTouchListener(this);
    //
    mGestureDetector = new GestureDetector(this, this);
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
    if (motionEvent.getAction() == MotionEvent.ACTION_MOVE)
    {
        Rect but1 = new Rect();
        bt1.getHitRect(but1);
        if (but1.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
        {
            bt1.setBackgroundColor(getColor(R.color.colorAccent));
            return true;
        }
        Rect but2 = new Rect();
        bt2.getHitRect(but2);
        if (but2.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
        {
            bt2.setBackgroundColor(getColor(R.color.colorAccent));
            return true;
        }
        Rect but3 = new Rect();
        bt3.getHitRect(but3);
        if (but3.contains((int) motionEvent.getX(), (int) motionEvent.getY()))
        {
            bt3.setBackgroundColor(getColor(R.color.colorAccent));
            return true;
        }
    }
    return true;
}

我测试了这段代码,它运行良好。
当然,如果你有200个按钮,最好在onCreate()中加载矩形,而不是每次触摸事件都创建它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接