我是android的初学者,这是我的第一篇文章。
在我的应用程序中,我有2个活动,其中一个是background_Setting,它的.xml是background_main。 在我的.xml中有按钮,当我点击任何人它改变了这个活动的背景但我想改变整个应用程序所有活动不在background_main只有
我该怎么做?提前致谢
package com.example.back;
import android.support.v7.app.ActionBarActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
public class backgroundSetting extends ActionBarActivity implements OnClickListener
{
ImageButton BlackeButton;
ImageButton WhiteButton;
ImageButton BlueeButton;
ImageButton GreenButton;
RelativeLayout myLO;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.backgrounds);
BackGroundBlac();
BlackeButton.setOnClickListener(this);
WhiteButton.setOnClickListener(this);
BlueeButton.setOnClickListener(this);
GreenButton.setOnClickListener(this);
}
private void BackGroundBlac()
{
myLO=(RelativeLayout)findViewById(R.id.backgroundActivity);
BlackeButton=(ImageButton)findViewById(R.id.black);
WhiteButton=(ImageButton)findViewById(R.id.white);
BlueeButton=(ImageButton)findViewById(R.id.blue);
GreenButton=(ImageButton)findViewById(R.id.green);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.black:
myLO.setBackgroundColor(Color.BLACK);
break;
case R.id.white:
myLO.setBackgroundColor(Color.WHITE);
break;
case R.id.blue:
myLO.setBackgroundColor(Color.BLUE);
break;
case R.id.green:
myLO.setBackgroundColor(Color.GREEN);
break;
}
}
}
答案 0 :(得分:0)
您可以在onCreate
中创建一个BroadcastReceiver:
BroadcastReceiver mColorChangedReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
int color = intent.getIntExtra("color", -1);
myLO.setBackgroundColor(color);
}
};
在生命周期方法中注册/取消注册:
@Override
public void onResume()
{
super.onResume();
LocalBroadcastManager.getInstance(getActivity())
.registerReceiver(mColorChangedReceiver, new IntentFilter("com.example.app.COLOR_CHANGED"));
}
@Override
public void onPause()
{
super.onPause();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mColorChangedReceiver);
}
当用户点击更改颜色时,发送广播:
Intent intent = new Intent("com.example.app.COLOR_CHANGED");
intent.putExtra("color", Color.BLACK);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);