我在我的活动中使用了开关盒。我使用6个案例,他们打开相同的活动Map.java。 现在在Map.java中,我希望从这个活动打开并希望在那里进行烘烤的按钮。 请给我路。提前。 这是我的代码
public void buttonClick(View v) {
switch (v.getId()) {
case R.id.imageButton1:
img3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Report.this, Map.class));
}
});
break;
case R.id.imageButton2:
img4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Report.this, Map.class));
}
});
break;
case R.id.imageButton3:
img5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Report.this, Map.class));
}
});
break;
case R.id.imageButton4:
img6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Report.this, Map.class));
}
});
break;
case R.id.imageButton5:
img7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Report.this, Map.class));
}
});
break;
case R.id.imageButton6:
img8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Report.this, Map.class));
}
});
break;
}
}
答案 0 :(得分:4)
您需要创建一个包并将参数传递给第二个活动:
public void buttonClick(View v) {
Intent intent = new Intent(Report.this, Map.class);
intent.putExtra("button", v.getId());
startActivity(intent);
}
在第二项活动中:
public class Map extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int caller = getIntent().getIntExtra("button", 0);
switch (caller) {
case R.id.imageButton1:
//do something
break;
case R.id.imageButton2:
//do something
break;
case R.id.imageButton3:
//do something
break;
case R.id.imageButton4:
//do something
break;
case R.id.imageButton5:
//do something
break;
case R.id.imageButton6:
//do something
break;
}
}
}
答案 1 :(得分:1)
尝试:
putExtra()
Intent方法将Button名称置于意图中。例如(第2步):
Intent intent = new Intent(Report.this, Map.class);
intent.putExtra("WhichButton", "ButtonName");
startActivity();