我有两个活动activity1和activity2。在activity2我有一个由几种颜色填充的微调器,我想要做的是当我在微调器中选择一种颜色时,它改变了活动1内的textview的颜色,当我在相同的活动中执行它时,它不会&# 39;任何帮助都将不胜感激。
答案 0 :(得分:0)
在"设置"
中使用这样的共享偏好设置//create SharedPreferences
SharedPreferences sharedPreferences = this.getSharedPreferences("nameYourSharedPref", Context.MODE_PRIVATE);
//get what color is selected from spinner and add it to SharedPreferences as a String for example
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("color", "red");
editor.apply();
在您的" MainActivity"得到这样的颜色
//create SharedPreferences
SharedPreferences sharedPreferences = this.getSharedPreferences("nameYourSharedPref", Context.MODE_PRIVATE);
//this will return the color you saved before or "defaultValue"...
String userSelectedColor = sharedPreferences.getString("color", "defaultValue");
//code to change the color wherever you want
if(userSelectedColor.equals("defaultValue"))
{
//don't change anything
}
else if(userSelectedColor.equals("red"))
{
//change color to red
}
答案 1 :(得分:0)
在Activity1中使用此intent方法
Intent i=new Intent(Edit_News.this,Activity.class);
startActivityForResult(i,2);
**在Activity2 **
在微调器中选择颜色并在选择颜色后使用此颜色并在此中传递颜色代码
Intent intent = new Intent();
intent.putExtra("color",#fffff);
setResult(Activity.RESULT_OK, intent);
finish();
现在,在您的activity1中,当activity2完成时,此方法将调用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
super.onActivityResult(requestCode, resultCode, result);
switch (requestCode) {
case 2:
if (resultCode == RESULT_OK)
{
Bundle bundle = result.getExtras();
int colorcod= bundle.getDouble("color");
mTextView.setTextColor(Color.parseColor(colorcod));
}
break;
}
}