我正在制作类似于说汤姆的游戏应用程序。在我开始新游戏时选择角色性别后,它应该在所有其他活动中显示所选图像(在ImageView中)。我设法只为主屏幕做,所以我需要一个想法如何让它在所有其他人可见。这是代码:
MainActivity:
public class MainActivity extends Activity implements OnClickListener {
Button bNew;
Button bContinue;
Button bFemale;
Button bMale;
Context context = this;
View view = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
initListeners();
}
private void initialize() {
bNew = (Button) findViewById(R.id.bNew);
bContinue = (Button) findViewById(R.id.bContinue);
}
private void initListeners() {
bNew.setOnClickListener(this);
bContinue.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bNew:
Dialog dialog = new Dialog(context, R.style.FullHeightDialog);
dialog.setContentView(R.layout.activity_dialog);
Button bFemale = (Button) dialog.findViewById(R.id.bFemale);
bFemale.setBackgroundResource(R.drawable.bfemale);
Button bMale = (Button) dialog.findViewById(R.id.bMale);
bMale.setBackgroundResource(R.drawable.bmale);
bFemale.setOnClickListener(this);
bMale.setOnClickListener(this);
dialog.show();
bFemale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLiving(R.drawable.bfemale);
}
});
bMale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startLiving(R.drawable.bmale);
}
});
break;
case R.id.bContinue:
startLiving();
break;
}
}
public void startLiving() {
Intent i = new Intent();
i.setClass(this, LivingActivity.class);
startActivity(i);
}
public void startLiving(int gender) {
Intent i = new Intent();
i.setClass(this, LivingActivity.class);
i.putExtra("gender", gender);
startActivity(i);
}
}
LivingActivity:
public class LivingActivity extends Activity implements OnClickListener {
Button bBack;
Button bKitchen;
Button bBathroom;
Button bBedroom;
Button bGarden;
ImageView daisy;
ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_living);
initialize();
initListeners();
}
private void initialize() {
this.bBack = (Button) findViewById(R.id.bBack);
this.bKitchen = (Button) findViewById(R.id.bKitchen);
this.bBathroom = (Button) findViewById(R.id.bBathroom);
this.bBedroom = (Button) findViewById(R.id.bBedroom);
this.bGarden = (Button) findViewById(R.id.bGarden);
daisy = (ImageView) findViewById(R.id.ivImage);
daisy.setImageResource(getIntent().getIntExtra("gender", 0));
daisy.setDrawingCacheEnabled(false);
}
private void initListeners() {
bBack.setOnClickListener(this);
bKitchen.setOnClickListener(this);
bBathroom.setOnClickListener(this);
bBedroom.setOnClickListener(this);
bGarden.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bBack:
startMain();
break;
case R.id.bKitchen:
startKitchen();
break;
case R.id.bBathroom:
startBathroom();
break;
case R.id.bBedroom:
startBedroom();
break;
case R.id.bGarden:
startGarden();
break;
}
}
// menu
private void startMain() {
Intent i = new Intent();
i.setClass(this, MainActivity.class);
startActivity(i);
}
private void startKitchen() {
Intent i = new Intent();
i.setClass(this, KitchenActivity.class);
startActivity(i);
}
private void startBathroom() {
Intent i = new Intent();
i.setClass(this, BathroomActivity.class);
startActivity(i);
}
private void startBedroom() {
Intent i = new Intent();
i.setClass(this, BedroomActivity.class);
startActivity(i);
}
private void startGarden() {
Intent i = new Intent();
i.setClass(this, GardenActivity.class);
startActivity(i);
}
}
答案 0 :(得分:0)
您可以使用其中任何一种
1. specify the drawable as a public static variable.
2. Pass the drawable name as an intent extra and get the corresponding image whereever you want them.
//pass the image name
Intent i = new Intent(this, LivingActivity.class);
i.putExtra("gender", gender);
startActivity(i);
Get corresponding image in onCReate() of LivingActivity
getResources().getDrawable(this.getIntent().getStringExtra("gender"));