我想在按钮被点击或聚焦时更改其背景图片。
这是我的代码:
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button tiny = (Button)findViewById(R.id.tiny);
tiny.setBackgroundResource(R.drawable.a9p_09_11_00754);
TextView txt = (TextView)findViewById(R.id.txt);
txt.setText("!---- On click ----!");
}
});
这段代码正确吗?它在事件中调用了一个按钮吗?
android:background="@drawable/selector_drawable"
编程方式:v.setBackgroundResource(R.drawable.selector_drawable)
尝试您和@Chirag Raval的答案,唯一的区别是选择器文件中的内容。我知道很久以前我使用了您的实现,并且它起作用了,而今天重用该代码却没有起作用,所以这可能与API有关。 - Jacob R