单击按钮时更改按钮的drawableLeft

Cel*_*lta 3 android onclick button xml-drawable android-drawable

我有Button一个drawableLeft,当我点击它,我想只有改变drawableleft为另一幅图像.我必须使用什么java代码?

XML:

<Button
    android:id="@+id/docli_btn_apagar"
    android:layout_width="200dp"
    android:layout_height="70dp"
    android:background="@android:color/transparent"
    android:onClick="ApagarLinha"
    android:drawableLeft="@drawable/trash"
    android:text="@string/apagar" 
    android:textAppearance="?android:attr/textAppearanceLarge"/>
Run Code Online (Sandbox Code Playgroud)

Bry*_*nny 6

这将在你的按钮的onClick()处理程序中调用

// get your button
Button docli_btn_apagar = (Button) findViewById(R.id.docli_btn_apagar );

// get the drawable
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );

// set the drawable left
docli_btn_apagar.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null );
Run Code Online (Sandbox Code Playgroud)

或者:

// You can skip the drawable object directly, by using ints/IDs
docli_btn_apagar.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)