我想在菜单中设置一个普通按钮,作为一个开关按钮,如切换按钮。但我不确定如何让一个按钮像开关一样?
switch(item.getItemId()){
case R.id.switcher:
View view = this.getWindow().getDecorView();
view.setBackgroundColor(Color.parseColor("#80FFFFFF"));
//I want to change the color of the background by clicking once
//and set the background color back to normal. How will I achieve this ?
return true;
答案 0 :(得分:1)
试试这个:
@Override
public void onClick(View v) {
if ((v.getId() == R.id.my_button){
buttonOnClick(v);
}
}
private void buttonOnClick(View v) {
switch (v.getId()) {
case R.id.my_button: {
if (v.isSelected()) {
// is selected, deselect!
v.setSelected(false);
//do your staff here
} else {
// is not selected, select!
v.setSelected(true);
//do your staff here
}
break;
}
default:
break;
}