10

我想做一些 B.logicActivity.onTouchEvent()方法。它适用于TextView其他不可点击的视图。但是,它不会在单击 时被调用Button。但我也需要那个。

这是代码。

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Oh shit!! click performed... :(", Toast.LENGTH_LONG).show();

            }
        });



    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "ssssssssssssssssssssssss:touch");
        return super.onTouchEvent(event);
    }

    public boolean dispatchTouchEvent(MotionEvent event) {
        int eventaction=event.getAction();

        switch(eventaction) {
        case MotionEvent.ACTION_MOVE:
            break;
        default:
            break;
        }

        return super.dispatchTouchEvent(event);
    }

}

请帮我解决这个问题。

4

5 回答 5

19

我根据需要使用了 dispatchTouchEvent(MotionEvent ev)。这是解决方案。

private boolean isInSideClicked = false;
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View content = findViewById(R.id.textView1);
        int[] contentLocation = new int[2];
        content.getLocationOnScreen(contentLocation);
        Rect rect = new Rect(contentLocation[0], 
                             contentLocation[1], 
                             contentLocation[0] + content.getWidth(), 
                             contentLocation[1] + content.getHeight());
        
        View frame = findViewById(R.id.editText1);
        int[] frameLocation = new int[2];
        frame.getLocationOnScreen(frameLocation);
        Rect framerect = new Rect(frameLocation[0], 
                                  frameLocation[1], 
                                  frameLocation[0] + frame.getWidth(), 
                                  frameLocation[1] + frame.getHeight());
        Log.d(TAG, "rect:  "+rect.bottom+" , "+rect.top+" , "+rect.left+" , "+rect.right);
        Log.d(TAG, "FrameRect:  "+framerect.bottom+" , "+framerect.top+" , "+framerect.left+" , "+framerect.right);
        Log.d(TAG, "x: "+event.getX()+"  y: "+event.getY());
        
        if ((rect.contains((int)event.getX(), (int)event.getY()) || framerect.contains((int)event.getX(), (int)event.getY()) ) ) {
            isInSideClicked = true;
        }
        if (isInSideClicked){
            return super.dispatchTouchEvent(event); 
        } else {
            return true;
        }
    } else if (event.getAction() == MotionEvent.ACTION_UP && isInSideClicked) {
        isInSideClicked = false;
        return super.dispatchTouchEvent(event);
    } else if (event.getAction() == MotionEvent.ACTION_MOVE && isInSideClicked) {
        return super.dispatchTouchEvent(event);
    } else {
        isInSideClicked = false;
        return true; 
    }
}

以下是操作系统级别如何处理触摸事件的层次结构。

Activity.dispatchTouchEvent()
ViewGroup.dispatchTouchEvent()
View.dispatchTouchEvent()
View.onTouchEvent()
ViewGroup.onTouchEvent()
Activity.onTouchEvent()

有了这个,我限制了不在所需布局/视图中的视图的触摸事件。

于 2013-04-01T14:06:30.430 回答
1

您在哪里为按钮注册了 touchListener。您只为按钮实现了 clickListener。要为按钮调用 onTouchEvent(),您必须将按钮注册为: button.setOnTouchListener(this);

于 2013-04-01T10:08:46.303 回答
0
 Please add below given 2 line in your xml hope it will help you its working fine for me.

    **android:focusable="false"
    android:focusableInTouchMode="false"**


     <Button
            android:id="@+id/button2"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/button1"
            android:layout_marginTop="3dp"
            android:background="#FFFFFF"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:text="Delete"
            android:textColor="#0099CC" />
于 2013-04-01T09:45:04.210 回答
0

请在您的 onCreate 方法中添加以下代码。

在此您可以获取按钮的向下、移动或向上事件。

当 Button up 事件调用时写下你的逻辑。

findViewById(R.id.btn).setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Toast.makeText(Main.this,
                        "Oh shit!! click down performed... :(",
                        Toast.LENGTH_LONG).show();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                Toast.makeText(Main.this,
                        "Oh shit!! click up performed... :(",
                        Toast.LENGTH_LONG).show();
                break;
            default:
                break;
            }
            return true;
        }
    });
于 2013-04-01T09:58:28.883 回答
0

我有同样的问题。这很容易解决,无需删除您的:

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG, "ssssssssssssssssssssssss:touch");
    return super.onTouchEvent(event);
};

添加到按钮:

button.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //YOUR CODE HERE
        if(v==button){
           //Your button has been pressed
        }
        else {  
          //Other View element has been pressed
             }
    }
}
于 2013-10-19T18:33:35.387 回答