【问题标题】:Save state of Image Button保存图像按钮的状态
【发布时间】:2017-07-29 16:33:25
【问题描述】:

好的,所以我有一个包含许多图像按钮的应用程序,我目前正在工作的是所有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();

}

}

【问题讨论】:

    标签: java imagebutton oncreate onsaveinstancestate onrestoreinstancestate


    【解决方案1】:

    在活动中移动时按钮的状态不会被保留,因为活动对象每次都会重新实例化,所以为了修复它,只需将其设为static变量即可。

    private static boolean isCandyPressed = false;
    

    希望这能解决您的问题

    【讨论】:

    • 我最终解决了上述代码在我的糖果图像按钮上工作的问题,我使用了共享首选项和 onpause 并在恢复时保存实例状态。
    • @JKnight 您能否在不使用此类范围的情况下检查此解决方案是否适合您,如果可以,请将其视为有效答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多