0

对于自定义按钮,使用什么代码来更改该按钮的文本/图片。例如,一旦图片变为一个事物,我单击按钮,然后再次单击它,图片将变为另一个事物。我怎么做?

4

2 回答 2

0

您可以使用选择器 xml 更改按钮的背景,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/bg_active"
      android:state_pressed="true" />
<item android:drawable="@drawable/bg_pressed"
      android:state_focused="true" />
<item android:drawable="@drawable/bg_focused" />
</selector>

您可以将它们放在项目的 Drawable 文件夹中。

您可以通过在按钮的 onClickListener 中设置它来更改单击事件上按钮的文本。

于 2013-04-11T06:29:36.270 回答
0

尝试这个:

int i=0;
btnRes.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
if(i%2==0){
    btnRes.setBackgroundResource(R.drawable.mypic);
i++;
}
else
{
btnRes.setBackgroundResource(R.drawable.yourpic);
i++;
        }
    });
于 2013-04-11T06:40:40.810 回答