2012-04-19 81 views
3

我一直在努力尋找StackOverflow或其他地方的解決方案,但我找不到任何直接發佈有關此特定問題的帖子,可能除了可能最接近的一個我再也找不到了。不過,如果這只是因爲我忽略了某些東西,或者我只是一個很大的小菜鳥,提前很抱歉。ViewFlipper的onTouch事件不會被觸發,而其子按鈕的onClick事件是

無論如何,我想將一個ViewFlipper(父母)和setOnClickListener的OnTouchListener設置爲一個按鈕(子),它填充父級佈局明智。我想先調用ViewFlipper的onTouch()事件。如果ViewFlipper的onTouch()返回false,則認爲Button的onClick()被觸發。但是,只調用Button的onClick()。爲什麼?有沒有明確的優先權?因爲Button有'match_parent'屬性,所以觸摸這個viewflipper與觸摸這個按鈕幾乎是一樣的,即使我這樣做了,它只是讓Button的onClick() )事件不發火....

以下是我的簡化活動;

public class TestActivity extends Activity implements OnTouchListener, View.OnClickListener { 
@Override public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ViewFlipper vf = (ViewFlipper) findViewById(R.id.viewFlipper1); 
    Button btn = (Button) this.findViewById(R.id.btnTest1); 
    vf.setOnTouchListener(this); 
    btn.setOnClickListener(this); 

} 

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ViewFlipper 
    android:id="@+id/viewFlipper1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <include 
     android:id="@+id/button" 
     layout="@layout/test" /> 
</ViewFlipper> 
</LinearLayout> 

的test.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/testLinear" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:baselineAligned="false" 
android:orientation="vertical" > 
<Button 
    android:id="@+id/btnTest1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="Button" /> 
</LinearLayout> 

回答

0

我還是不明白自己爲何ViewFlipper的onTouch事件不是第一次叫,但我找到了一種方法有點逃避這個問題。我們可以將onClickListener和onTouchListener都設置爲按鈕。

btn.setOnClickListener(this); 
btn.setOnTouchListener(this); 

正如在原來的帖子中談到的,它可以防止onClick()事件發生,但是什麼?如果它沒有被android系統調用,那麼我們應該手動調用它。

@Override public boolean onTouch (View v, MotionEvent event) { 

// walls of codes 

this.onClick(v); 

我知道我需要一些調整。如果用戶花費0.5秒以上將手指從屏幕上移開,我不會稱之爲'點擊'。