简体   繁体   English

ACTION_UP在Action_DOWN之后无法识别

[英]ACTION_UP does not recognize after Action_DOWN

In my project i need to use the onTouchListener in order for me to know when the user touch\\don't touch specific buttons and send data to my server in both cases. 在我的项目中,我需要使用onTouchListener ,以便让我知道用户何时触摸\\不触摸特定按钮,并且在两种情况下都将数据发送到我的服务器。 the code: 编码:

 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;
}
};

i tried to add action_cancle - the problem didnt solve yet. 我试图添加action_cancle-问题尚未解决。 basicly what happend is that i only recieve the action down event \\ data that i send and the if of action up never recognize. 基本上发生了什么事,就是我只收到我发送的动作下降事件\\数据,而如果动作上升则永远无法识别。 i checked all over the internet and the common answer was to return true in every case of onTouch function which makes sense but this is exacly what i do and still cant make it to work. 我在整个互联网上进行了检查,常见的答案是在每种情况下都返回true的onTouch函数,这很有意义,但这确实是我的工作,但仍然无法使其正常工作。

on control: 在控制之下:

 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);
    }
}

layout: 布局:

<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" />

setting the listeners: 设置监听器:

 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);

}

If you're using the view inside a view group, then maybe the ViewGroup is taking control over your touch intercepts, 如果您使用的是视图组中的视图,则可能是ViewGroup正在控制您的触摸截取,

try: requestDisallowInterceptTouchEvent(true); 试试: requestDisallowInterceptTouchEvent(true);

inside the place where you are listening for your ACTION_DOWN event. 在您正在听ACTION_DOWN事件的地方。

Edit your onControl() method to: 编辑您的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);
}} 

Im not sure of this, but is worth a try. 我对此不确定,但值得一试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM