2014-02-08 34 views
0

我在做什麼時,我創建編輯文本視圖創建交叉(X)按鈕動態。我要的是,一旦,一個編輯文本視圖會出現一個小按鈕也將獲得出現在它的右(有(X)的圖像(所以,我可以刪除特定編輯文本視圖)。我能夠生成編輯文本視圖,但不知道關於按鈕的創建。請指導或幫助我。每個編輯文本查看我創造一個按鈕的點擊動態

回答

0

您可以簡單地創建一個新的按鈕與EditText視圖相同的方式與一個x在它對齊它的EditText字段的右側,然後做按鈕的onclicklistener,將調用到DB或任何你正在使用刪除的實際文本,然後就可以刪除這兩個從佈局中查看和編輯按鈕。不過,從你的代碼提供我不知道看到關於正在實施onClickListener什麼。

0

這是我實現

<CustomEditText 
    android:id="@+id/edittext1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:drawableRight="@drawable/ic_action_cancel" 
    android:ems="10" 
    android:focusable="true" 
    android:singleLine="true" 
    android:textColor="#000" /> 

其中ic_action_cancelx圖標。 和下面是自定義EDITTEXT類

public class CustomEditText extends EditText { 

    private Drawable drawableRight; 
    int actionX, actionY; 

    public CustomEditText(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public CustomEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomEditText(Context context) { 
     super(context); 
    } 

    @Override 
    public void setCompoundDrawables(Drawable left, Drawable top, 
      Drawable right, Drawable bottom) { 
     if (right != null) { 
      drawableRight = right; 
     } 
     super.setCompoundDrawables(left, top, right, bottom); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     Rect bounds; 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      actionX = (int) event.getX(); 
      actionY = (int) event.getY(); 
      // this works for left since container shares 0,0 origin with bounds 
      if (drawableRight != null) { 
       bounds = null; 
       bounds = drawableRight.getBounds(); 
       int x, y; 
       int extraTapArea = 13; 
       int extraTapArea = 20; 
       x = (int) (actionX + extraTapArea); 
       y = (int) (actionY - extraTapArea); 
       x = getWidth() - x; 
       if (x <= 0) { 
        x += extraTapArea; 
       } 
       if (y <= 0) 
        y = actionY; 
       if (bounds.contains(x, y)) { 
        setText(""); 
        return false; 
       } 
       return super.onTouchEvent(event); 
      } 
     } 
     return super.onTouchEvent(event); 
    } 
} 
相關問題