【问题标题】:Long press definition at XML layout, like android:onClick doesXML 布局中的长按定义,就像 android:onClick 一样
【发布时间】:2011-04-18 16:38:03
【问题描述】:

有什么方法可以像 onClick 那样在 XML 布局中定义 longKeyLongPress 定义?。

即这是我的观点

<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"

 />

我想要以前的东西,但对长按事件有反应。

注意:

  • 我不想从我的代码中添加监听器。

  • 我尝试使用 android:longClickable。

【问题讨论】:

    标签: android


    【解决方案1】:

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

    1. 扩展 TextView,我们称之为 MyTextView
    2. 然后在res/values/中添加文件attrs.xml,内容如下:

      <xml version="1.0" encoding="utf-8"?>
      <resources>
          <declare-styleable name="MyTextView">
              <attr name="onKeyLongPress" format="string"/>
          </declare-styleable>
      </resources>
      
    3. 在 MyTextView constructor 中添加从 xml 读取数据的逻辑:

      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();
      
      }
      
    4. 在布局 xml 中使用新属性:

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

    学分:

    【讨论】:

      【解决方案2】:

      查看当前文档,目前不存在这样的 XML 参数。 longClickable 是一个布尔参数,用于简单地定义 View 是否响应长点击。

      【讨论】:

      • 自己看文档也得出了同样的结论,即似乎没有XML属性来设置onLongClickListener,必须用代码来完成。
      • 我不知道...您将不得不做您不想做的事情...将侦听器添加到您的代码中。
      【解决方案3】:

      (10 年后,可能对其他人有用)

      当使用 Databinding 和 MVVM 时,您可以编写一个按预期工作的 Bindingadapter:

         @BindingAdapter("android:onLongClick")
         fun setOnLongClickListener(view: View,block : () -> Unit) {
              view.setOnLongClickListener {
                  block()
                  return@setOnLongClickListener true
              }
          }
      

      然后你可以像这样使用它:android:onLongClick="@{() -&gt; vm.yourFunction()}"

      如果您在某些情况下想要返回 false,也可以返回函数并将 Unit 更改为布尔值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-20
        • 2022-01-17
        • 1970-01-01
        • 2015-11-23
        • 1970-01-01
        • 1970-01-01
        • 2011-11-14
        • 2011-10-11
        相关资源
        最近更新 更多