【发布时间】:2014-11-29 19:17:54
【问题描述】:
在我的开始(主)屏幕上,我有 3 个按钮。 Button_1、Button_2 和 Button_3。它们都打开相同的Activity B。所以在这里我需要根据在主屏幕上单击哪个按钮在第二次活动后打开不同的Activity C。我可以用 2 个按钮完成它,但是当我尝试使用带有 elseif 的第三个按钮时,它无法正常工作。这是我尝试的方法。
主屏幕
Button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("isButtonClicked",true);
startActivity(intent);
}
});
Button_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("button", true);
startActivity(intent);
}
});
Button_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("isButtonClicked",false);
startActivity(intent);
}
});
这里是SecondActivity
boolean isButton = getIntent().getBooleanExtra("isButtonClicked",false);
boolean button = getIntent().getBooleanExtra("button",true);
if(isButton)
{
Intent newActivity = new Intent(SecondActivity.this, ThirdActivity.class);
newActivity.putExtra("Position", Position);
newActivity.putExtra("resultServer", resultServer);
newActivity.putExtra("text", MyArrList.get(position).get("text").toString());
newActivity.putExtra("name", MyArrList.get(position).get("name").toString());
startActivity(newActivity);
}
else if (button)
{
Intent newActivity = new Intent(SecondActivity.this, FourthActivity.class);
newActivity.putExtra("Position", Position);
newActivity.putExtra("resultServer", resultServer);
newActivity.putExtra("id", MyArrList.get(position).get("id").toString());
startActivity(newActivity);
}
else
{
Intent newActivity = new Intent(SecondActivity.this, FifthActivity.class);
newActivity.putExtra("Position", Position);
newActivity.putExtra("resultServer", resultServer);
newActivity.putExtra("id", MyArrList.get(position).get("id").toString());
startActivity(newActivity);
}
正如您在第二个活动之后看到的,所有 3 个按钮都相同,然后我想加载不同的。如果我把它放在if{}else{} 上,即两个按钮效果很好。但现在我在Button_2 和Button_3 上得到相同的结果。
【问题讨论】:
-
same result on button 2 and 3... 哪个区块? -
elseif{}else{}... 两个按钮都打开FifthActivity.class。
标签: android android-intent android-activity