2017-10-06 78 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); 
}