在XML布局上长按定义,就像android:onClick一样

在XML布局上长按定义,就像android:onClick一样,android,Android,有任何方法可以像onClick那样定义XML布局longKeyLongPress定义 i、 这是我的观点 <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:text="Line 1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/message" andr

有任何方法可以像onClick那样定义XML布局longKeyLongPress定义

i、 这是我的观点

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:text="Line 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/message"
android:textSize="15dip"
android:textStyle="bold"
android:textColor="@color/colorblue"
android:shadowDy="1.0"
android:shadowDx="1.0"
android:shadowRadius="1.0"
android:shadowColor="#ffffffff"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="5dip"
android:lineSpacingExtra="3dip"
android:lineSpacingMultiplier="1.1"
android:singleLine="false"
android:autoLink="web|email|phone|map|all"

android:onClick="clickHandler"
android:clickable="true"

 />

我想要像以前一样的东西,但对longpress事件做出反应

注:

  • 我不想从代码中添加侦听器

  • 我试过android:长点击


查看当前文档,此类XML参数当前不存在。longClickable是一个布尔参数,用于简单定义视图是否响应长时间单击。

该属性未定义,但您可以实现它

  • 扩展TextView,我们称之为MyTextView
  • 然后将文件attrs.xml添加到res/values/中,并包含以下内容:

    <xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="MyTextView">
            <attr name="onKeyLongPress" format="string"/>
        </declare-styleable>
    </resources>
    
    
    
  • 构造函数的代码段(稍作修改)取自原始的android视图类
    • (10年后,可能对其他人有用)

      使用数据绑定和MVVM时,您可以编写一个按预期工作的Bindingadapter: 然后可以像这样使用它:
      android:onLongClick=“@{()->vm.yourFunction()}”


      如果您在某些情况下indend返回false,您也可以将函数和更改单位返回为boolean

      只是我自己查看了文档并得出了相同的结论,即似乎没有XML属性来设置onLongClickListener,并且必须使用代码来完成。我不知道。。。你将不得不做你不想做的事。。。将侦听器添加到代码中。
      public MyTextView(final Context context, final AttributeSet attrs) {
      super(context, attrs);
      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
      
      for (int i = 0; i < a.getIndexCount(); ++i)
      {
          int attr = a.getIndex(i);
          switch (attr)
          {
              case R.styleable.MyTextView_onKeyLongPress: {
                  if (context.isRestricted()) {
                      throw new IllegalStateException("The "+getClass().getCanonicalName()+":onKeyLongPress attribute cannot "
                              + "be used within a restricted context");
                  }
      
                  final String handlerName = a.getString(attr);
                  if (handlerName != null) {
                      setOnLongClickListener(new OnLongClickListener() {
                          private Method mHandler;
      
                          @Override
                          public boolean onLongClick(final View p_v) {
                              boolean result = false;
                              if (mHandler == null) {
                                  try {
                                      mHandler = getContext().getClass().getMethod(handlerName, View.class);
                                  } catch (NoSuchMethodException e) {
                                      int id = getId();
                                      String idText = id == NO_ID ? "" : " with id '"
                                              + getContext().getResources().getResourceEntryName(
                                                  id) + "'";
                                      throw new IllegalStateException("Could not find a method " +
                                              handlerName + "(View) in the activity "
                                              + getContext().getClass() + " for onKeyLongPress handler"
                                              + " on view " + MyTextView.this.getClass() + idText, e);
                                  }
                              }
      
                              try {
                                  mHandler.invoke(getContext(), MyTextView.this);
                                  result = true;
                              } catch (IllegalAccessException e) {
                                  throw new IllegalStateException("Could not execute non "
                                          + "public method of the activity", e);
                              } catch (InvocationTargetException e) {
                                  throw new IllegalStateException("Could not execute "
                                          + "method of the activity", e);
                              }
                              return result;
                          }
                      });
                  }
                  break;
              }
              default: 
                  break;
          }
      }
      a.recycle();
      
      }
      
      <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:custom="http://schemas.android.com/apk/res/res-auto"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          >
      
          <your.package.MyTextView
              android:id="@+id/theId"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              custom:onKeyLongPress="myDoSomething"
          />
          <!-- Other stuff -->
      </LinearLayout>
      
         @BindingAdapter("android:onLongClick")
         fun setOnLongClickListener(view: View,block : () -> Unit) {
              view.setOnLongClickListener {
                  block()
                  return@setOnLongClickListener true
              }
          }