3

我有一个ButtondrawableLeft当我点击它时,我只想更改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"/>
4

2 回答 2

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 );

或者:

// You can skip the drawable object directly, by using ints/IDs
docli_btn_apagar.setCompoundDrawablesWithIntrinsicBounds(R.drawable.smiley, 0, 0, 0);
于 2013-04-24T14:13:00.980 回答
0
public class AsyncClassTest extends Activity {

String strMessage;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.docli_btn_apagar);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            ((Button) view).setCompoundDrawables(getResources().getDrawable(android.R.drawable.btn_plus), null, null, null);
        }
    });
}
}
于 2013-04-24T14:17:34.780 回答