2017-10-06 77 views
-4

我希望我的Button使用意图打开新活动。 这我已经实施,它的工作原理。Android - 按钮更改新活动的背景图片

我的问题是为这个新的活动设置背景图像。

例如:

  • 按钮1时按下得到了新的活动打开与此搜索作为 背景。
  • 当按钮2被按下时,新的活动以image2打开为 背景。

如果有人会知道Java代码来实现此功能或我可以找到它,会是非常好的。

+0

传递每个按钮点击的唯一值,意在知道哪个按钮被按下。 –

+0

将活动中的包值传递给第二个活动。在第二个活动中获得该捆绑并根据它做出决定。 –

+0

非常感谢! – Taka

回答

0

你可以做这样的事情。呼吁两国按钮,点击下面的功能,并在你的第二个活动为Button1 = 1和按钮2 = 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到您的下一个活动上按一下按钮使用

intent.putExtra("button","button1"); 

int whichButtonPressed = getIntent().getIntExtra("BUTTON_PRESSED", 0); 

if (whichButtonPressed == 1) 
{ 
    // Show image 1 
} 
else if (whichButtonPressed == 2) 
{ 
    // Show Image 2 
} 
0

发送值。然后通过使用

String msg = intent.getStringExtra("button"); 

然后通过检查值来执行操作。

样品

在Button1的Click

Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
intent.putExtra("button","button1"); 
startActivity(intent); 

在BUTTON2点击

Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
intent.putExtra("button","button2"); 
startActivity(intent); 

然后,在你SecondActivity的onCreate()方法

String msg = intent.getStringExtra("button"); 

if (msg.equals("button1")) 
{ 
    //yourlayout.setImageBacground(image1); 
} 
else 
{ 
    //yourlayout.setImageBacground(image2); 
}