0

好了,所以我有一個有許多圖像按鈕,我現在有工作是所有onClick聽衆,並有當用戶點擊圖像的圖像更改應用程序。我閱讀和研究,我可以用onSavedInstance()onRestore()保存這些圖像按鈕的狀態。這就是我現在所擁有的,我只是試圖爲一個圖像按鈕,因爲如果這是不正確的,我不想改變所有。保存狀態

我也有讓我感動的,通過各活動按鈕,問題是,當我移動到另一個活動,然後搬回圖像按鈕更改不會保存按鈕圖像可以追溯到如果從未按下它。

我有一種感覺,我失去了一些東西很簡單,因爲我已經觀看和閱讀一些教程,但仍無法得到它的工作。如果能幫助我理解我會感激的。

沒關係問題解決了,我只嘗試了使用共享偏好,並把該進的暫停和恢復對我的形象的一個按鈕。我現在可以按下回車鍵並退出應用程序而不會丟失數據。

public boolean isPressed; 
private boolean isCandyPressed = false; 
private static final String CANDY_PRESSED = "CandyPressed"; 
private SharedPreferences prefs; 
private String PrefName = "MyPref"; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_desserts); 


    vegetablesButton = (ImageButton) findViewById(R.id.vegetabesButton); 


    candy = (ImageButton) findViewById(R.id.candy); 
    chocolate = (ImageButton) findViewById(R.id.chocolate); 
    cookies = (ImageButton) findViewById(R.id.cookies); 
    cupcakes = (ImageButton) findViewById(R.id.cupcakes); 
    doughnuts = (ImageButton) findViewById(R.id.doughnuts); 
    iceCream = (ImageButton) findViewById(R.id.icecream); 
    jello = (ImageButton) findViewById(R.id.jello); 
    marshMellows = (ImageButton) findViewById(R.id.marshmellows); 
    muffins = (ImageButton) findViewById(R.id.muffins); 
    pudding = (ImageButton) findViewById(R.id.pudding); 
    yogurt = (ImageButton) findViewById(R.id.yogurt); 
    vegetablesButton = (ImageButton) findViewById(R.id.vegetabesButton); 


    ImageChanger(chocolate, R.drawable.buttongrabchocolate, R.drawable.chocolate); 
    ImageChanger(cookies, R.drawable.buttongrabcookies, R.drawable.cookies); 
    ImageChanger(cupcakes, R.drawable.buttongrabcupcakes, R.drawable.cupcake); 
    ImageChanger(doughnuts, R.drawable.buttongrabdoughnuts, R.drawable.doughnut); 
    ImageChanger(iceCream, R.drawable.buttongrabicecream, R.drawable.icecream); 
    ImageChanger(jello, R.drawable.buttongrabjello, R.drawable.jelly); 
    ImageChanger(marshMellows, R.drawable.buttongrabmarshmellows, R.drawable.marshmellows); 
    ImageChanger(muffins, R.drawable.buttongrabmuffins, R.drawable.muffin); 
    ImageChanger(pudding, R.drawable.buttongrabpudding, R.drawable.pudding); 
    ImageChanger(yogurt, R.drawable.buttongrabyogurt, R.drawable.yogurt); 


    setCandyImage(); 
    candy.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      if (!isCandyPressed) { 
       candy.setBackgroundResource(R.drawable.buttongrabcandy); 
       isCandyPressed = true; 
      } else { 
       candy.setBackgroundResource(R.drawable.candy); 
       isCandyPressed = false; 

      } 
     } 
    }); 
    vegetablesButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent myIntent = new Intent(Desserts.this, Items.class 
      ); 
      Desserts.this.startActivity(myIntent); 
     } 
    }); 

} 


protected void setCandyImage() { 
    if (isCandyPressed) { 
     candy.setBackgroundResource(R.drawable.buttongrabcandy); 


    } else { 
     candy.setBackgroundResource(R.drawable.candy); 
    } 
} 

void ImageChanger(final ImageButton a, final int b, final int c) { 
    a.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      if (isPressed) { 
       a.setBackgroundResource(b); 

      } else 
       a.setBackgroundResource(c); 
      isPressed = !isPressed; 


     } 
    }); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    prefs=getSharedPreferences(PrefName,MODE_PRIVATE); 
    SharedPreferences.Editor editor=prefs.edit(); 

    editor.putBoolean(CANDY_PRESSED,isCandyPressed); 
    editor.commit(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    prefs=getSharedPreferences(PrefName,MODE_PRIVATE); 
    isCandyPressed=prefs.getBoolean(CANDY_PRESSED,false); 
    setCandyImage(); 

} 

} 

回答

0

按鈕的狀態通過活動移動時不會保留,因爲活動對象每次重新實例化,所以爲了解決這個問題,你只要把它static變量。

private static boolean isCandyPressed = false; 

希望這能解決你的問題

+0

我最終搞清楚上面的代碼工作在我的糖果圖像按鈕,我使用的共享首選項,在onPause和簡歷保存實例狀態的問題。 –

+0

@JKnight您可以檢查該解決方案適用於您不使用這樣的程度,如果是認爲這是一個有效的答案。 –