2015-12-14 64 views
1

我可以在Android easly中添加一個drawableRight,EditText,並且點擊事件可以在一個drawableRight的情況下完美工作。但我需要兩個drawableRightEditText如何在EditText中添加兩個drawableRight?

那麼,如何在EditText中添加兩個drawableRight?並且我還需要分別在drawableRight上執行單擊事件。

例如我想添加如下圖所示的EditText中的黃色星星,並點擊最右邊的圖像我想打開手機的通訊錄並點擊黃色星號我想打電話給用戶最喜歡的號碼列表。

那麼我該如何做到這一點?任何想法?

multiple drawable

+0

你需要使用relativeLayout。 – k0sh

+0

使用relativeLayout並在你的圖像中設置一個在relativeLayout下面的行。 – Destro

回答

0

你不能。 TextView只能在其兩側包含一個drawable。您唯一的選擇是:

  1. 創建自定義View
  2. 取一些ViewGroup後代(RelativeLayout/FrameLayout/etc),並將TextView和兩個ImageView一起放入其中。
2

有這個沒有原生支持,所以你來到這裏兩個選項:

簡單的解決方案:創建一個線性佈局,右側拖圖像視圖,這些將是你的可繪製。

難的方法:擴展Drawable類,並實現自己的onDraw方法,在那裏你會吸取上述兩種可繪製。比用你的文本視圖。

+0

如果你採取'硬'的方式 - 這實際上並不那麼困難 - 確保也實現了setBounds來爲兩個drawable都留出空間。 – yedidyak

+0

雖然如果你想爲每個drawable使用不同的點擊方法,簡單的方法就是要走。 – yedidyak

-1

只需使用RelativeLayout將兩個Drawable放入EditText。 要設置內填充,放置一個隱形drawableRight到的EditText:

/res/values/dimens.xml

<resources> 
    <dimen name="iconSize">32dp</dimen> 
</resources> 

/res/layout/my_layout.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:inputType="textAutoComplete"/> 

    <ImageButton 
     android:id="@+id/imageButton1" 
     android:layout_width="@dimen/iconSize" 
     android:layout_height="@dimen/iconSize" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/ic_action_1"/> 

    <ImageButton 
     android:id="@+id/imageButton2" 
     android:layout_width="@dimen/iconSize" 
     android:layout_height="@dimen/iconSize" 
     android:layout_toLeftOf="@+id/imageButton1" 
     android:layout_centerVertical="true" 
     android:src="@drawable/ic_action_2"/> 

</RelativeLayout> 

In your Activity:

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_layout); 

    EditText editText = (EditText) findViewById(R.id.editText); 

    int iconSize = (int) getResources().getDimension(R.dimen.iconSize) 

    Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_action_1); 
    drawable.setBounds(0, 0, iconSize * 2, 0); // that is the trick! 

    editText.setCompoundDrawables(null, null, drawable, null); 

} 
相關問題