2012-04-19 12 views
3

私はStackOverflowなどでソリューションを見つけるのに苦労していますが、この特定の問題について直接の投稿は見つかりませんでした私がもはや見つけることができない最も近いもの。しかし、もし私が何かを見落としただけであれば、私はあまりにも大きな騒ぎです。ViewFlipperのonTouchイベントは、子ボタンのonClickイベントが発生していないときに発生します

とにかく、OnTouchListenerをViewFlipper(親)に設定し、setOnClickListenerを親(親)レイアウトを塗りつぶしたボタン(子)に設定しようとしています。私はViewFlipperのonTouch()イベントを最初に呼びたいと思っていました。 ViewFlipperのonTouch()がfalseを返した場合、ButtonのonClick()が発生したと考えられます。ただし、ボタンのonClick()だけが呼び出されました。どうして?定義済みの優先順位はありますか?

また、Buttonに 'match_parent'属性が設定されているため、onTouchListenerを設定することができます。このビューフッタをタッチすることは、このボタンをタッチすることと事実上同じです(ボタンのonClick )イベントunfired ....

以下は私の簡単なアクティビティです。

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()イベントは発生しませんが、それ以外は何ですか?アンドロイドシステムによって呼び出されない場合は、手動で呼び出す必要があります。

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

// walls of codes 

this.onClick(v); 

私はいくつかの調整が必要であることを知っています。ユーザーが画面から指を離すのに0.5秒以上かかると「クリック」しない。