我一直在stackoverflow或其他地方努力寻找解决方案,但是我找不到任何关于这个问题的直接帖子,也许除了我已经找不到的最近的一个。但是,如果这仅仅是因为我忽略了什么或者我只是一个大傻瓜,请提前道歉。
无论如何,我试图将ontouchListener设置为viewFlipper(父级),并将setonClickListener设置为按钮(子级),以填充父级布局。我希望首先调用viewFlipper的ontouch()事件。如果viewFlipper的ontouch()返回false,则button的onclick()被触发。但是,只调用了按钮的onclick()。为什么?有明确的优先权吗?
或者,我可以将OntouchListener设置为按钮,因为按钮具有“match_parent”属性,所以触摸这个ViewFlipper实际上与触摸这个按钮是一样的,即使我这样做了,它只是使按钮的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);

}

MIN .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>

最佳答案

我仍然不明白为什么viewflipper的ontouch事件没有被首先调用,但是我找到了一种方法来逃避这个问题。我们可以将onclicklistener和ontouchlister设置为按钮。

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

正如在最初的文章中所说的,它可以防止onclick()事件的发生,但是无论如何?如果Android系统没有调用它,那么我们应该手动调用它。
@Override public boolean onTouch (View v, MotionEvent event) {

// walls of codes

this.onClick(v);

我知道我需要一些调整。如果用户用超过0.5秒的时间将手指从屏幕上取下,我不会称之为“点击”。

10-07 12:50