Java Android-按钮更改新活动的背景图像
我希望我的按钮使用意图打开一个新活动。 我已经实现了这一点,并且它可以正常工作 我的问题是为这个新活动设置背景图像 例如: 按下按钮1时,新活动打开,图像1为 出身背景 按下按钮2时,新活动打开,图像2为 出身背景Java Android-按钮更改新活动的背景图像,java,android,image,button,background,Java,Android,Image,Button,Background,我希望我的按钮使用意图打开一个新活动。 我已经实现了这一点,并且它可以正常工作 我的问题是为这个新活动设置背景图像 例如: 按下按钮1时,新活动打开,图像1为 出身背景 按下按钮2时,新活动打开,图像2为 出身背景 如果有人知道实现此功能的Java代码或我在哪里可以找到它,那就太好了。您可以这样做。在两个按钮上调用下面的函数,并传递button1=1和button2=2的值 /* * Button 1 press then pass = 1 * Button 2 press then p
如果有人知道实现此功能的Java代码或我在哪里可以找到它,那就太好了。您可以这样做。在两个按钮上调用下面的函数,并传递button1=1和button2=2的值
/*
* Button 1 press then pass = 1
* Button 2 press then pass = 2
* */
private void buttonPressed(int value)
{
Intent intent = new Intent(YourActivity.this, SecondActivity.class);
intent.putExtra("BUTTON_PRESSED", value);
startActivity(intent);
}
现在,在第二个活动中,只需从Intent中获取值
int whichButtonPressed = getIntent().getIntExtra("BUTTON_PRESSED", 0);
if (whichButtonPressed == 1)
{
// Show image 1
}
else if (whichButtonPressed == 2)
{
// Show Image 2
}
使用Intent将值发送到您的下一个活动
intent.putExtra("button","button1");
String msg = intent.getStringExtra("button");
按一下按钮。然后使用获取另一个活动的值
intent.putExtra("button","button1");
String msg = intent.getStringExtra("button");
然后,通过检查值来执行操作
样品
点击按钮1
在按钮2上单击
然后,在第二个Activity onCreate方法中
通过单击每个按钮传递唯一值,以了解按下了哪个按钮。将捆绑值从您的活动传递到第二个活动。在第二个活动中,获取该捆绑包并根据它做出决策。
String msg = intent.getStringExtra("button");
if (msg.equals("button1"))
{
//yourlayout.setImageBacground(image1);
}
else
{
//yourlayout.setImageBacground(image2);
}