【问题标题】:How to Implement OnTouch Listener in XML如何在 XML 中实现 OnTouch 监听器
【发布时间】:2015-12-17 04:49:57
【问题描述】:

据我所知,我们可以在 xml 中编写后在 xml 中定义 onClick 标记,我们可以通过在 xml 中指定的名称轻松地在 java 代码中使用,例如

<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:onClick="myClick" />

public void myClick(View v) {
    // does something very
}

1) 有什么方法可以在 XML 中定义onTouch 如果是,那么如何使用???

2) 或任何其他方式在onClick 侦听器中实现onTouch 侦听器;

我的目标是在不定义按钮名称的情况下与 100 多个按钮进行交互,如下所述:并且还具有 onTouch 功能......

    Button mbutton;
    mbutton = (Button)findViewbyId(R.id.button);

谢谢

【问题讨论】:

  • 基本上,您可以为此定义自定义属性。
  • @cricket_007 好的,但我不想像你提到的那样使用视图类EditText editText1;
  • @NigamPatro 请给我一个如何定义自定义属性的例子。??
  • @Attaullah:在类中简单实现View.OnTouchListener,其中扩展Button类在构造函数中添加this.setOnTouchListener(this);

标签: android onclicklistener android-xml ontouchlistener


【解决方案1】:

1) 有什么方法可以在 XML 中定义 onTouch 如果是,那么如何使用???

,android没有提供默认属性让你在xml文件中定义触摸事件,你必须在.java文件中编写方法。

2) 或任何其他方式在 onClick 监听器中实现 onTouch 监听器;

,您不能在onClick() 中实现onTouch(),因为当您第一次触摸或说单击或点击屏幕时会调用onClick 事件,并在您释放触摸时执行该事件。但它无法检测到您的触摸移动,即ACTION_DOWNACTION_UP 等。

所以如果你想实现onTouch()事件,你就得自己写了。

你可以编写一些机制让你轻松实现onTouch()事件。但我建议您不要这样做,而是使用Butterknife 库,它可以让您通过定义annotation 轻松编写onTouch() 方法。 Here's the code 他们是如何在注解中绑定 onTouch() 的(以防你想编写自己的机制)。

【讨论】:

  • acucally 我需要在 onclick 中使用 ACTION_DOWN 和 UP,但条件是不定义 View 类,例如 Button mButton 直接访问方法。正如我的问题中提到的那样。
【解决方案2】:
Hope this helps some one

You can easily do it using databinding:

step1

public class DataBidingAdapter{

 @SuppressLint("ClickableViewAccessibility")
    @BindingAdapter("touchme")
    public static void setViewOnTouch(TextView view, View.OnTouchListener listener) {
       view.setOnTouchListener(listener);
    }
}

step 2 in xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>       
        <variable
            name="handlers"
            type="com.user.handlers.Handlers" />

    </data>

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">  
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
          >

            <EditText
                android:id="@+id/search"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/search_for_chats_title"
                app:touchme="@{handlers.searchPatient}" />
        </FrameLayout>
</linearLayout>
</layout>
step 3
 public class Handler{
 public boolean searchPatient(View v, MotionEvent event) {
        if (MotionEvent.ACTION_UP == event.getAction()) {
          //your code
        }
        return true;
    }
}

【讨论】:

  • @mochadwi 应该吗?也许这在 2015 年不存在。
【解决方案3】:

据我所知,您不能将代码直接放入 XML。如果我错了,请纠正我。您必须在 Activity 类中有代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 2011-02-02
    • 1970-01-01
    • 2013-07-08
    • 2013-05-13
    • 1970-01-01
    • 2019-04-01
    相关资源
    最近更新 更多