@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 19:49:25 浏览: 147
@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;
}
相关问题
public class MyGridView extends GridView{ public MyGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public MyGridView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub int expandSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }该代码什么意思
这段代码是自定义的一个GridView类,继承自Android系统的GridView类。它重写了父类的三个构造方法,并且重写了父类的onMeasure方法。
在构造方法中,通过调用父类的构造方法来初始化GridView实例。
而在重写的onMeasure方法中,通过改变MeasureSpec的值来实现GridView的高度自适应。这里使用了一个expandSpec的值,通过将其设置为MeasureSpec.AT_MOST和Integer.MAX_VALUE >> 2(即最大值的四分之一),确保GridView在测量时的高度不会超过其子项的高度总和,从而实现了GridView的自适应高度。
/定义接口 【】 IPet{ void beFriendly(); void beAccompanied(); } //定义抽象类 【】 class Animal{ private String name; //定义抽象方法 public 【】 void move(); public Animal(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } //继承animal 实现IPet class Cat 【】{ public Cat(String name){ 【】 } //实现beFriendly方法 //实现be beAccompanied方法 @Override public void move() { // TODO Auto-generated method stub System.out.println("cat move quiently"); } } //继承animal 实现IPet class Dog 【】{ public Dog(String name){ 【】 } //实现beFriendly方法 //实现be beAccompanied法 @Override public void move() { // TODO Auto-generated method stub System.out.println("dog move quickly"); } }
答案如下:
定义接口:
```java
interface IPet {
void beFriendly();
void beAccompanied();
}
```
定义抽象类:
```java
abstract class Animal {
private String name;
public Animal(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract void move();
}
```
继承Animal类实现IPet接口的Cat类:
```java
class Cat extends Animal implements IPet {
public Cat(String name) {
super(name);
}
@Override
public void beFriendly() {
System.out.println("Cat is friendly");
}
@Override
public void beAccompanied() {
System.out.println("Cat likes to be accompanied");
}
@Override
public void move() {
System.out.println("Cat moves quietly");
}
}
```
继承Animal类实现IPet接口的Dog类:
```java
class Dog extends Animal implements IPet {
public Dog(String name) {
super(name);
}
@Override
public void beFriendly() {
System.out.println("Dog is friendly");
}
@Override
public void beAccompanied() {
System.out.println("Dog likes to be accompanied");
}
@Override
public void move() {
System.out.println("Dog moves quickly");
}
}
```
阅读全文