0

我在幾個ImageButtons和代碼實現自來水事件的其他觸摸事件看起來是這樣的觸地的佈局阻止按鈕

MyTouchListener touchListener = new MyTouchListener(); 
     l1.setOnTouchListener(touchListener); 
     r1.setOnTouchListener(touchListener); 
     l2.setOnTouchListener(touchListener); 
     r2.setOnTouchListener(touchListener); 

public class MyTouchListener implements View.OnTouchListener { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      switch (v.getId()) { 
       case R.id.l1: 
        clickL1(v); 
        break; 
       case R.id.r1: 
        clickR1(v); 
        break; 
       case R.id.l2: 
        clickL2(v); 
        break; 
       case R.id.r2: 
        clickR2(v); 
        break; 
      } 
      return true; 
     } 

     return false; 
    } 

我所遇到的問題是,當我在佈局(按住一個手指的任何地方除外圖像按鈕),當我按住按鈕時,按鈕不會註冊任何事件。

我試圖給我的佈局自己的觸摸偵聽器返回false,但沒有奏效。

我該怎麼做才能「忽略」佈局上的觸碰事件,並且只需要正常聽取imagebutton事件?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:id="@+id/relativeLayout" 
    android:splitMotionEvents="true" 
    tools:context="com.example.josip.something.GameOffline"> 


    <ImageButton 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/l1" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:focusableInTouchMode="true"/> 

    <ImageButton 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/r1" 
     android:layout_above="@+id/l1" 
     android:layout_toRightOf="@+id/l1" 
     android:layout_toEndOf="@+id/l1" 
     android:focusableInTouchMode="true"/> 

    <ImageButton 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/r2" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:focusableInTouchMode="true"/> 

    <ImageButton 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/l2" 
     android:layout_above="@+id/r2" 
     android:layout_toLeftOf="@+id/r2" 
     android:layout_toStartOf="@+id/r2" 
     android:focusableInTouchMode="true"/> 


<RelativeLayout> 
+0

請將您的xml –

+0

@KirillK在那裏:) – user6754277

回答

0

所以我找到了解決方案。我把另一個RelativeLayout作爲主佈局的子元素,並作爲按鈕的一部分。我將它設置爲可點擊,這種方式在我按住佈局的任何位置時都可以工作,並且我嘗試單擊按鈕,事件觸發。爲什麼呢,我不知道,如果有人願意,我想要一個解釋。

<RelativeLayout 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:id="@+id/relativeLayout" 
    android:splitMotionEvents="true" 
    tools:context="com.example.josip.something.GameOffline"> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:clickable="true"></RelativeLayout> 

    <ImageButton 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/l1" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:focusableInTouchMode="true"/> 

    <ImageButton 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/r1" 
     android:layout_above="@+id/l1" 
     android:layout_toRightOf="@+id/l1" 
     android:layout_toEndOf="@+id/l1" 
     android:focusableInTouchMode="true"/> 

    <ImageButton 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/r2" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:focusableInTouchMode="true"/> 

    <ImageButton 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:id="@+id/l2" 
     android:layout_above="@+id/r2" 
     android:layout_toLeftOf="@+id/r2" 
     android:layout_toStartOf="@+id/r2" 
     android:focusableInTouchMode="true"/> 


<RelativeLayout> 
0

編輯:android:splitMotionEvents="true"在含有按鈕的佈局。

而不是event.getAction()嘗試MotionEventCompat.getActionMasked(event)獲取與正確的指針索引關聯的操作。閱讀更多關於它的地方:enter link description here

而且,只有在您確實處理了一個事件(if { }聲明內)時,您才應該返回true。否則返回false

+0

使用您提供的例子,我發現雖然我手指放在佈局上並嘗試觸摸按鈕,事件DOES註冊(作爲一個多事件),但觸摸不是簡單的按鈕..任何想法如何改變? – user6754277

+0

有趣的一點。嘗試設置'android:focusableInTouchMode =「true」'或在xml(或'button.setFocusableInTouchMode')中播放類似的屬性 –

+0

另一種可能是嘗試使用'TouchDelegate' https://developer.android.com/reference/android /view/TouchDelegate.html,將獲得觸摸事件,並執行點擊按鈕,而不是直接添加OnTouchEventListeners –

0

更換returntrueonTouchfalse
通過這樣做,您可以告訴android將事件傳遞給下一個處理程序(例如onClick)。

+0

我實際上做了這是一個嘗試由@Kirill K建議,但沒有奏效。我的xml現在看起來就像我自己的答案所建議的那樣,而java正如問題所示。它的工作原理,我只是不知道爲什麼 – user6754277