如何从另一个活动中获取背景?

时间:2015-05-07 09:59:12

标签: java android colors background

我想在我的“设置 - 活动”中更改另一个“活动”(开始)的背景颜色(在不同的颜色,声音等之间进行选择)。

如何更改其他活动的颜色?

我试图从我的开始 - 活动(id:开始)获取背景:

    final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);

但它似乎无效。

设置活动的完整代码:

public class activity_settings extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_settings);
    ColorChange();
}

public void ColorChange() {
    final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);

    final RadioButton ChangeToBlue = (RadioButton) findViewById(R.id.button_blue);
    final RadioButton ChangeToRed = (RadioButton) findViewById(R.id.button_red);


    final Button button_save = (Button) findViewById(R.id.button_save);
    button_save.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            ChangeOption(background);
        }
    });


    ChangeToBlue.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            ChangeToBlue(background);
        }
    });

    ChangeToRed.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            ChangeToRed(background);
        }

    });


}



public void ChangeOption(RelativeLayout background) {
    if (background.isEnabled()) {
        background.setEnabled(false);
    } else {
        background.setEnabled(true);

    }
}

public void ChangeToBlue(RelativeLayout background) {
    background.setBackgroundColor(Color.BLUE);
    background.invalidate();

}
public void ChangeToRed(RelativeLayout background) {
    background.setBackgroundColor(Color.RED);
    background.invalidate();

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_activity_settings, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

1 个答案:

答案 0 :(得分:1)

在sharedPreference中保存设置活动中的颜色代码,即#F0F0F0,并在要设置背景的活动的onCreate()中获取此值。