如何在Android中使用onclick更改我的按钮的背景可绘制?

时间:2015-05-30 18:00:03

标签: android button onclick drawable

我有一个带有可绘制XML背景的按钮

android:background="@drawable/greenbut"

我想通过点击我的按钮将背景更改为@drawable/yellowbut

我尝试使用此代码,但似乎完全错误。我无法使用setBackground Function。你能帮忙吗?

public void colorChanger(View v)
{
    Button changeBut = (Button) findViewById(R.id.button2);
    changeBut.setBackground(R.drawable.yellowbut);
    changeBut.isClickable();
}

2 个答案:

答案 0 :(得分:2)

试试这个

// HTML

<canvas id="scene"></canvas>

// Draggable setup

$('.scene-item').draggable({
    helper: 'clone',
    appendTo: 'body',
    containment: 'window',
    scroll: false,
    zIndex: 9999
});

// Drop Setup

var canvas = new fabric.Canvas('scene');

canvas.setDimensions({
    width: '800px',
    height: '500px'
}, {
    cssOnly: true
});
document.getElementById("scene").fabric = canvas;

canvas.on({
    'object:moving': function(e) {
        e.target.opacity = 0.5;
    },
    "object:modified": function(e) {
        e.target.opacity = 1;
    }
});


$('#scene').droppable({
    drop: function(e, ui) {
        if ($(e.target).attr('id') === 'scene') {
            var pointer = canvas.getPointer();
            fabric.Image.fromURL(ui.draggable[0].currentSrc, function (img) {
                console.log(img);
                img.set({
                    left: pointer.x - 20,
                    top: pointer.y - 20,
                    width: 52,
                    height: 52,
                    scale: 0.1
                });
                //canvas.clear();
                canvas.add(img);
                canvas.renderAll();
            });
        }
    }

});

答案 1 :(得分:2)

它就像按照代码一样简单,希望它能帮助

setContentView(R.layout.activity_main);
        final Button changeBut = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                button.setBackgroundColor(Color.yellow);
            }
        });