代码之家  ›  专栏  ›  技术社区  ›  Jean Valjean

android:ontouchevent()和onclick事件

  •  0
  • Jean Valjean  · 技术社区  · 6 年前

    我有一个项目,在这个项目中我重写了ontouchevent()并注册taps并在其中移动。屏幕上还有按钮。我正在尝试将方法连接到按钮,即:

    public void onButtonClick(View view) {
        System.out.println("Here");
    }
    

    我可以将方法连接到按钮,连接显示在XML中:

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onButtonClick"
        android:text="Button"
        tools:layout_editor_absoluteX="89dp"
        tools:layout_editor_absoluteY="139dp" />
    

    但点击按钮似乎没有注册。是因为我也重写了ontouchevent()?如果是的话,我应该在MotionEvents中注册点击吗?如果是这样的话,有什么简单的方法可以区分来自多个按钮的点击?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Sagar    6 年前

    onTouch() 方法返回 true onClickListener

    false

    onTouch(final View v, final MotionEvent event)

    View v.getId()

    @Override
    public boolean onTouch(final View v, final MotionEvent event) {
        if(v.getId()==R.id.button1){
            //Click is coming from Button with id button1 (specified in layout.xml as android:id="@+id/button1"). 
            //Do something for click
        }
        ...
    }