@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.item1: Intent intent=new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); this.finish(); break; case R.id.item2: this.finish(); break; default: break; } return true; } }向这段代码中添加int id = item.getItemId(); if (id == R.id.myButton) { // 更换 app 背景 Drawable newBackground = getResources().getDrawable(R.drawable.new_background); getWindow().setBackgroundDrawable(newBackground);
时间: 2024-03-01 18:49:25 浏览: 143
Android--开发--音量seekbar制作.rar
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId(); // 添加这一行代码
switch (id) {
case R.id.item1:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
this.finish();
break;
case R.id.item2:
this.finish();
break;
case R.id.myButton: // 添加新的 case 语句
Drawable newBackground = getResources().getDrawable(R.drawable.new_background);
getWindow().setBackgroundDrawable(newBackground);
break;
default:
break;
}
return true;
}
阅读全文