Java 根据按下的按钮加载捆绑包
我在一个类中有两个按钮,这些按钮将您发送到同一个类,但取决于按下的按钮,它将显示不同的文本/图像/按钮 所以现在,当按下一个按钮时,它会将您发送到下一个类并添加一些INTET,如下所示:Java 根据按下的按钮加载捆绑包,java,android,Java,Android,我在一个类中有两个按钮,这些按钮将您发送到同一个类,但取决于按下的按钮,它将显示不同的文本/图像/按钮 所以现在,当按下一个按钮时,它会将您发送到下一个类并添加一些INTET,如下所示: button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Bundle bu
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle1 = new Bundle();
bundle1.putInt("top", R.drawable.1);
bundle1.putInt("mid", R.drawable.2);
bundle1.putInt("bot", R.drawable.3);
Intent intet1 = new Intent(curclass.this, nextclass.class);
intent1.putExtras(bundle1);
startActivity(intent1);
Bundle asd = getIntent().getExtras();
int asdasd = asd.getInt("top");
im1 = (ImageView) findViewById(R.id.imagetop);
im1.setImageResource(newimage);
bu1 = (Button) findViewById(R.id.buttontop);
bu1.setText("blahblah");
在下一个类中,每个按钮都有一个bundle,但我相信我需要在它们上设置if/else,以确保它只使用其中一个bundle,因为它现在不能正常工作
那么我该怎么做“如果”这个词呢?
我已经试过了,但我不知道在“如果”后面加什么。
差不多
"if (button1 = pressed)
(do this)
else if (button2 = pressed)
(do this)"
提前谢谢
抱歉,如果我像一个idoit一样解释,可能是因为我是其中之一,而且我刚刚开始编程
编辑:问题是,我在第二个活动中有两个包,如下所示:
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle1 = new Bundle();
bundle1.putInt("top", R.drawable.1);
bundle1.putInt("mid", R.drawable.2);
bundle1.putInt("bot", R.drawable.3);
Intent intet1 = new Intent(curclass.this, nextclass.class);
intent1.putExtras(bundle1);
startActivity(intent1);
Bundle asd = getIntent().getExtras();
int asdasd = asd.getInt("top");
im1 = (ImageView) findViewById(R.id.imagetop);
im1.setImageResource(newimage);
bu1 = (Button) findViewById(R.id.buttontop);
bu1.setText("blahblah");
我把mid和bot放在下面,然后我有第二个bundle,它告诉button2的int显示什么图像/文本,问题是即使我按下button1,它也会加载第二个bundle的内容,我怀疑这是因为bundle只是并排出现在那里,并告诉它们做什么
我希望这能把事情弄清楚:)得到。。。方法,您可以定义一个默认值 把这个放在你想放的任何地方,也许你有一个常量类,或者你最好把它放在哪里。因为它是静态的,所以您可以随时随地访问它。 创建“全局变量”,唯一标识按钮
public static final int no_button = -1;
public static final int button_1 = 1;
public static final int button_2 = 2; //you are not forced to use -1, 1, 2, just use different numbers
始终在按下按钮后的第一个活动中
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle1 = new Bundle();
bundle1.putInt("button id", button_1);
bundle1.putInt("top", R.drawable.1);
bundle1.putInt("mid", R.drawable.2);
bundle1.putInt("bot", R.drawable.3);
Intent intet1 = new Intent(curclass.this, nextclass.class);
intent1.putExtras(bundle1);
startActivity(intent1);
或
然后在第二个活动中
Bundle asd = getIntent().getExtras();
switch(asd.getInt("button id", -1)){
case button_1:
[put here the code you want to execute if button1 was pressed]
case button_2:
[put here the code you want to execute if button2 was pressed]
case no_button:
[put here the code you want to execute if something else happened]
}
在第二个活动中,只有通过setExtras传递到Intent的bundle才可见。你能详细说明什么东西工作不正常,或者发布第二个活动中的相关代码吗?我编辑了OP,提供了更多信息:)你说“问题是,我在第二个活动中有两个包,看起来像这样:”。。。但我只看到一包。你能给我看另一个捆绑代码吗?谢谢你的回答!然而,这一部分我并不理解:“bundle.setInt(“button id”putton按下的id);“它去哪里了?它意味着什么?”我扩展了答案@user1050429。如果你觉得它有用,请接受它你是人类中的神!这个网站太棒了!