0

在我的项目中,我需要使用onTouchListener以便我知道用户何时触摸\不触摸特定按钮并将数据发送到我的服务器。编码:

 private View.OnTouchListener handleTouch = new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {

    switch (v.getId()) {
        case R.id.Abtn:
            onControl("BA",v, event);
            break;
        case R.id.Bbtn:
            onControl("BB", v, event);
            break;
        case R.id.Xbtn:
            onControl("BX",v, event);
            break;
        case R.id.Ybtn:
            onControl("BY",v, event);
            break;
        case R.id.Rbtn:
            onControl("BR",v, event);
            break;
        case R.id.Lbtn:
            onControl("BL",v, event);
            break;
        case R.id.UpBtn:
            onControl("BU",v, event);
            break;
        case R.id.Dbtn:
            onControl("BD",v, event);
            break;
        case R.id.RTbtn:
            onControl("RT",v, event);
            break;
        case R.id.LTbtn:
            onControl("LT",v, event);
            break;
        case R.id.Start_btn:
            onControl("ST",v, event);
            break;
        case R.id.Back_btn:
            onControl("CK",v, event);
            break;
        case R.id.RB_btn:
            onControl("RB",v, event);
            break;
        case R.id.LB_btn:
            onControl("LB",v, event);
            break;


    }
    return true;
}
};

我试图添加 action_cancle - 问题还没有解决。基本上发生的事情是,我只收到了我发送的动作停止事件\数据,而动作的 if 则永远无法识别。我检查了整个互联网,常见的答案是在 onTouch 函数的每种情况下都返回 true,这是有道理的,但这正是我所做的,但仍然无法使其工作。

控制:

 private void onControl(String st,View v, MotionEvent event){
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        new SendMessage().execute("S"+ st);
    }
    if(event.getAction() == MotionEvent.ACTION_UP){
        new SendMessage().execute("P "+ st);
    }
}

布局:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
    android:id="@+id/Bbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="20dp"
    android:layout_marginTop="84dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/button_b" />

<ImageButton
    android:id="@+id/Xbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="84dp"
    android:layout_marginStart="120dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/button_x" />

<ImageButton
    android:id="@+id/Abtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="134dp"
    android:layout_marginStart="70dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/button_a" />

<ImageButton
    android:id="@+id/Ybtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="34dp"
    android:layout_marginStart="70dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/button_y" />
<ImageButton
    android:id="@+id/RTbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="210dp"
    android:layout_marginTop="30dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/rt_button" />
<ImageButton
    android:id="@+id/LTbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="500dp"
    android:layout_marginTop="30dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/lt_button" />


<ImageButton
    android:id="@+id/UpBtn"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="500dp"
    android:layout_marginTop="230dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:rotation="-90"
    android:src="@drawable/sides_b" />

<ImageButton
    android:id="@+id/Rbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="465dp"
    android:layout_marginTop="265dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/sides_b" />

<ImageButton
    android:id="@+id/Lbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="535dp"
    android:layout_marginTop="265dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:rotation="180"
    android:src="@drawable/sides_b" />
<ImageButton
    android:id="@+id/Dbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="500dp"
    android:layout_marginTop="300dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:rotation="90"
    android:src="@drawable/sides_b" />

<ImageButton
    android:id="@+id/Start_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="300dp"
    android:layout_marginTop="50dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/start_btn" />
<ImageButton
    android:id="@+id/Back_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="420dp"
    android:layout_marginTop="50dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:rotation="180"
    android:src="@drawable/start_btn" />
<ImageButton
    android:id="@+id/LB_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="470dp"
    android:layout_marginTop="130dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/lb_btn" />
<ImageButton
    android:id="@+id/RB_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="210dp"
    android:layout_marginTop="130dp"
    android:background="#00000000"
    android:contentDescription="@string/desc"
    android:src="@drawable/rb_btn" />

设置监听器:

 private void initView(){
    ImageButton A =  findViewById(R.id.Abtn);
    ImageButton B = findViewById(R.id.Bbtn);
    ImageButton X =  findViewById(R.id.Xbtn);
    ImageButton Y =  findViewById(R.id.Ybtn);
    ImageButton Rside =  findViewById(R.id.Rbtn);
    ImageButton Lside =  findViewById(R.id.Lbtn);
    ImageButton Up =  findViewById(R.id.UpBtn);
    ImageButton Down =  findViewById(R.id.Dbtn);
    ImageButton Rt =  findViewById(R.id.RTbtn);
    ImageButton Lt =  findViewById(R.id.LTbtn);
    ImageButton Start = findViewById(R.id.Start_btn);
    ImageButton Back = findViewById(R.id.Back_btn);
    ImageButton Rb = findViewById(R.id.RB_btn);
    ImageButton Lb = findViewById(R.id.LB_btn);

    A.setOnTouchListener(handleTouch);
    B.setOnTouchListener(handleTouch);
    X.setOnTouchListener(handleTouch);
    Y.setOnTouchListener(handleTouch);
    Rside.setOnTouchListener(handleTouch);
    Lside.setOnTouchListener(handleTouch);
    Up.setOnTouchListener(handleTouch);
    Down.setOnTouchListener(handleTouch);
    Rt.setOnTouchListener(handleTouch);
    Lt.setOnTouchListener(handleTouch);
    Start.setOnTouchListener(handleTouch);
    Back.setOnTouchListener(handleTouch);
    Rb.setOnTouchListener(handleTouch);
    Lb.setOnTouchListener(handleTouch);

}
4

1 回答 1

0

如果您在视图组中使用视图,那么可能ViewGroup正在控制您的触摸拦截,

尝试: requestDisallowInterceptTouchEvent(true);

在您收听ACTION_DOWN活动的地方。

onControl()将您的方法编辑为:

private void onControl(String st,View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
   requestDisallowInterceptTouchEvent(true);
   new SendMessage().execute("S"+ st);
}
else if(event.getAction() == MotionEvent.ACTION_UP){
   requestDisallowInterceptTouchEvent(false);
   new SendMessage().execute("P "+ st);
}} 

我不确定这一点,但值得一试。

于 2018-04-04T12:57:35.753 回答