2011-04-18 22 views
13

有什麼方法可以定義到XML佈局longKeyLongPress定義像onClick那樣?在XML佈局的長按定義,如android:onClick確實

即這是我的看法

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

回答

5

查看當前文檔,這樣的XML參數當前不存在。 longClickable是一個布爾參數,用於簡單定義View是否響應長時間點擊。

+1

我一直在看自己的文檔,並得出了相同的結論,即似乎沒有XML屬性來設置onLongClickListener,它必須用代碼完成。 – Squonk 2011-04-18 16:58:05

+0

那麼,沒有解決方法來做到這一點? – vsm 2011-04-18 17:47:41

+0

我沒有意識到......你將不得不做你不想做的事......將偵聽器添加到你的代碼中。 – Maximus 2011-04-18 17:48:21

13

屬性未定義,但您可以實現它。

  1. 擴展TextView,我們稱之爲MyTextView
  2. 然後在RES /值/添加文件attrs.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(); 
    
    } 
    
  3. <xml version="1.0" encoding="utf-8"?> 
    <resources> 
        <declare-styleable name="MyTextView"> 
         <attr name="onKeyLongPress" format="string"/> 
        </declare-styleable> 
    </resources> 
    
  4. 在MyTextView 構造添加邏輯以從XML讀取數據在佈局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> 
    

現金

+0

太棒了!感謝:D – RominaV 2013-12-09 16:16:28