[英]How to transport ToggleButton to SecondActivity
我有一個問題。 我需要我的運輸TooggleButton
到SecondActivity
與保存的狀態,我完全不知道該怎么做。 如果MainActivity
我有這樣的事情:
public void changeButton(View view)
{
boolean on = ((ToggleButton) view).isChecked();
if(on)
{
view.setBackgroundColor(Color.rgb(59, 223, 59));
Intent intent = new Intent(this, Basket.class);
intent.putExtra(Basket.TOGGLE_ON_EXTRA, getToggleState());
startActivity(intent);
}
else
{
view.setBackgroundColor(Color.rgb(255, 100, 100));
}
}
和SecondActivity
public class Basket extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basket);
Intent intent = getIntent();
boolean toggleOn = intent.getBooleanExtra(TOGGLE_ON_EXTRA, false);
}
認真地,只有這部分工作正常
public void changeButton(View view)
{
boolean on = ((ToggleButton) view).isChecked();
if(on)
{
view.setBackgroundColor(Color.rgb(59, 223, 59));
}
else
{
view.setBackgroundColor(Color.rgb(255, 100, 100));
}
}
在第一個示例中,問題是TOGGLE_ON_EXTRA
和getToggleState()
。
開始下一個活動時,請多加一些。
startActivity(new Intent(Main.this, ActivitySecond.class).putExtra("state","on");
在第二個活動中,獲得像這樣的額外信息:
String state = getIntent().getExtras().getString("state");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.