我想更改 MainActivity 中的背景。颜色将来自另一项活动 Main3Activity 。我试图使用onActivityResult,但我没有经验。
在图片上 onClick 之后更改 Main3Activity
中的颜色任何人都可以帮助我吗?
Main3Activity 的屏幕截图:
http://i.stack.imgur.com/IJEfG.png][1]
MainActivity:
public class MainActivity extends AppCompatActivity {
RelativeLayout wallpaper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wallpaper = (RelativeLayout) findViewById(R.id.main_layout);
}
public void getwallpaper(View v) {
Intent Getwallpaper = new Intent(this, Main3Activity.class);
startActivityForResult(Getwallpaper, 2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
if (data != null) {
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item1) {
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
} else if (id == R.id.action_settings) {
Intent intent2 = new Intent(this, Main3Activity.class);
startActivity(intent2);
}
return super.onOptionsItemSelected(item);
}
}
Main3Activity:
import static android.graphics.Color.RED;
public class Main3Activity extends AppCompatActivity {
ImageView green;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
green = (ImageView) findViewById(R.id.green);
green.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("Color", GREEN);
setResult(2, intent);
finish();
}
});
}
}
答案 0 :(得分:0)
int colour = intent.getIntExtra("Color", Color.WHITE);
findViewById(android.R.id.content).setBackgroundColor(colour);
或
int colour = intent.getIntExtra("Color", Color.WHITE);
View view = this.getWindow().getDecorView();
view.setBackgroundColor(colour);