Long press definition at XML layout, like android:onClick does

前端 未结 2 501
时光说笑
时光说笑 2020-12-03 17:27

There is any way to define into XML layout longKeyLongPress definition like onClick does ?.

i.e this is my view



        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 17:45

    The attribute is not defined, however you can implement it.

    1. Extend TextView and let's call it MyTextView.
    2. Then add file attrs.xml in res/values/ with following content:

      
      
          
              
          
      
      
    3. In MyTextView constructor add logic to read data from 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. Use new attribute in your layout xml:

      
      
          
          
      
      

    Credits:

    • I have learned how to do this from this post: http://kevindion.com/2011/01/custom-xml-attributes-for-android-widgets/
    • Snippet for constructor with slight modifications was taken from original android View class.

提交回复
热议问题