【问题标题】:ACTION_UP does not recognize after Action_DOWN在 Action_DOWN 之后 ACTION_UP 无法识别
【发布时间】:2018-04-04 12:51:42
【问题描述】:

在我的项目中,我需要使用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);

}

【问题讨论】:

标签: android android-studio android-event android-touch-event


【解决方案1】:

如果您在视图组中使用视图,那么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);
}} 

我不确定,但值得一试。

【讨论】:

  • 当你想让我写那行时,我不太喜欢。(android和java新手)
  • 勾选编辑,添加这个控制方法,勾选一次。
  • "无法解析方法 requestDisallowTouchInterceptEvent(boolean)"
  • v.requestDisallowInterceptTouchEvent(); 我在回答时没有使用 IDE,所以 E&OE
  • 你能发布你的布局吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 2013-10-22
  • 2015-05-31
相关资源
最近更新 更多