【发布时间】:2021-11-26 19:52:59
【问题描述】:
我有两个主要活动和设置。我想使用设置中的按钮更改主的背景颜色。我尝试使用 RelativeLayout 并使用 id 获取主布局,并在触摸按钮时更改颜色,但它不起作用。我也尝试过使用 Intent,但我不知道该怎么做。得到下面设置活动的代码
public class Settings extends AppCompatActivity {
private Button changeColorButton;
private RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
changeColorButton = findViewById(R.id.changeColorButton);
layout = findViewById(R.id.mainLayout);
/** Called when the user taps the Settings button */
changeColorButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View V){
Intent intent = new Intent(Settings.this, Main.class);
startActivity(intent);
layout.setBackgroundColor(Color.BLUE);
}
});
}
}
有什么帮助吗?
【问题讨论】:
标签: java xml android-studio