用另一个活动更改图像按钮

时间:2015-03-21 15:38:04

标签: java android

我希望从另一个事件更改ImageButtons,但这不起作用 这是我的MainActivity:

boolean defaultValue = false; 

SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
boolean shouldChangeButton = prefs.getBoolean("shouldChangeButton", defaultValue);

if(shouldChangeButton) {
    ImageButton ib = (ImageButton) findViewById(R.id.imageButton10);
    ib.setVisibility(View.VISIBLE);
}

boolean defaultValue1 = false;

SharedPreferences prefs1 = getSharedPreferences("YourAppNamePrefs1",MODE_PRIVATE);
boolean shouldChangeButton1 = prefs1.getBoolean("shouldChangeButton1",defaultValue1);

if (shouldChangeButton1) {
    ImageButton ib1 = (ImageButton) findViewById(R.id.imageButton11);
    ib1.setVisibility(View.VISIBLE);
}

这是另一个事件:

public void onClick7 (View view) {
    Intent myIntent = new Intent(view.getContext(), historiak.class);

    SharedPreferences prefs1 = getSharedPreferences("YourAppNamePrefs1", MODE_PRIVATE);
    prefs1.edit().putBoolean("shouldChangeButton1", true).apply();

    startActivity(myIntent);
    finish();
    MediaPlayer mediaPlayer = MediaPlayer.create(historia20.this, R.raw.dobre);
    mediaPlayer.start();
}

public void onClick8 (View view) {
    Intent myIntent = new Intent(view.getContext(), zemk.class);

    SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
    prefs.edit().putBoolean("shouldChangeButton", true).apply();

    startActivity(myIntent);
    finish();
    MediaPlayer mediaPlayer = MediaPlayer.create(zem20.this, R.raw.dobre);
    mediaPlayer.start();
}

当我运行这个写的应用程序时:不幸的是应用已停止

请帮帮我

1 个答案:

答案 0 :(得分:0)

我正在查找您的代码,但我看不到您启动MainActivity的任何行。

将其更改为

public void onClick7 (View view) {
    Intent myIntent = new Intent(view.getContext(), MainActivity.class);

    SharedPreferences prefs1 = getSharedPreferences("YourAppNamePrefs1", MODE_PRIVATE);
    prefs1.edit().putBoolean("shouldChangeButton1", true).apply();

    startActivity(myIntent);
    finish();

}

public void onClick8 (View view) {
    Intent myIntent = new Intent(view.getContext(), MainActivity.class);

    SharedPreferences prefs = getSharedPreferences("YourAppNamePrefs", MODE_PRIVATE);
    prefs.edit().putBoolean("shouldChangeButton", true).apply();

    startActivity(myIntent);
    finish();

}