Java Android-从对话框中选择后如何在图像视图中进行可绘制显示
我正在制作一个类似于会说话的汤姆的游戏应用程序。当我在开始一个新游戏时选择一个角色性别后,它应该在所有其他活动的ImageView中显示选定的图像。我设法只在主屏幕上显示,所以我需要一个如何使它在所有其他屏幕上可见的想法。以下是代码: 主要活动:Java Android-从对话框中选择后如何在图像视图中进行可绘制显示,java,android,Java,Android,我正在制作一个类似于会说话的汤姆的游戏应用程序。当我在开始一个新游戏时选择一个角色性别后,它应该在所有其他活动的ImageView中显示选定的图像。我设法只在主屏幕上显示,所以我需要一个如何使它在所有其他屏幕上可见的想法。以下是代码: 主要活动: public class MainActivity extends Activity implements OnClickListener { Button bNew; Button bContinue; Button bFe
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);
}
}
生活活动:
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);
}
}
你可以用任何一个
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"));
谢谢你的回答。但是你怎么说把可提取的名称作为额外的目的呢?我对这一切还是新手,所以我真的不明白:编辑了我的答案。检查一下当我尝试按照你的答案时,我得到一个错误,说getExtra方法对于类型意图是未定义的。。我不知道这意味着什么,也不知道如何解决。。