gpt4 book ai didi

java - 主要 Activity 未设置主题

转载 作者:行者123 更新时间:2023-12-02 03:56:16 25 4
gpt4 key购买 nike

我正在尝试动态更改应用程序的主题。我有两项 Activity ,一项是主要 Activity ,另一项是设置 Activity 。

在设置 Activity 中,当选择颜色时,我将从颜色选择器对话框中更改主题,主题会更改。但主题仅针对设置 Activity 进行更改,而不针对主要 Activity 进行更改。

我尝试将颜色值从设置 Activity 发送到主 Activity ,但应用程序无法启动。它只显示空白屏幕。

当我进行调试时,我发现它卡在第一个 if(n==0){} 上,并且循环继续下去并没有结束。

不明白哪里出了问题。

当我来自设置 Activity 时,主要 Activity 也可能不会刷新。如何刷新主要 Activity ?

设置 Activity :

public class Settings extends AppCompatActivity {

int no;

public static final String MyPREFERENCES = "MyPrefs" ;
public static final int color = 0;
public static final int color_2 = 0;
public static final int color_3 = 0;
SharedPreferences sharedpreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {

Theme.onActivityCreateSetTheme(this);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);



final ColorPickerDialog colorPickerDialog = new ColorPickerDialog();
colorPickerDialog.initialize(R.string.dialog_title, new int[]{Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.GRAY, Color.YELLOW}, Color.YELLOW, 3, 2);
colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() {

@Override
public void onColorSelected(int color) {

if (color == Color.CYAN) {
Theme.changeToTheme(Settings.this, Theme.THEME_DEFAULT);
no = 0;
}
else if (color == Color.LTGRAY)

{
Theme.changeToTheme(Settings.this, Theme.THEME_WHITE);
no = 1;
}
else if (color == Color.BLACK) {

Theme.changeToTheme(Settings.this,Theme.THEME_BLUE);
no = 3;

}
Toast.makeText(Settings.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show();
}

});

SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putInt("color",no);
// editor.putInt("color_2",no);
// editor.putInt("color_3",no);
editor.commit();

LinearLayout theme = (LinearLayout)findViewById(R.id.theme);

theme.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");
}
});
}

}

主题:

    public class Theme {

private static int sTheme;

public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;

/** * Set the theme of the Activity, and restart it by creating a new Activity of the same type. */
public static void changeToTheme(Activity activity, int theme) {
sTheme = theme;
activity.finish();
Intent i = new Intent(activity,activity.getClass());
activity.startActivity(i);
}

/** Set the theme of the activity, according to the configuration. */
public static void onActivityCreateSetTheme(Activity activity) {
switch (sTheme) {
default:

case THEME_DEFAULT:
activity.setTheme(R.style.AppTheme);
break;

case THEME_WHITE:

activity.setTheme(R.style.AppTheme_Solarized);
break;

case THEME_BLUE:

activity.setTheme(R.style.BlueTheme);

break;

}
}
}

主要 Activity :

public class MainActivity extends AppCompatActivity {

private NavigationView navigationView;
private DrawerLayout drawerLayout;
Toolbar mToolbar;
private MainFragment mFragment;
private int no,no1,no2;

@Override
protected void onCreate(Bundle savedInstanceState) {

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();


no = pref.getInt("color",0);


if(no == 0)
{
setTheme(R.style.AppTheme);
}

else if(no == 1)
{
setTheme(R.style.AppTheme_Solarized);
}

else if(no == 2)
{
setTheme(R.style.BlueTheme);
}

// Theme.onActivityCreateSetTheme(this);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

谢谢。

最佳答案

在您的Settings类中,您将选定的颜色写入“MyPrefs”首选项,但在您的MainActivity中,您正在从读取>“MyPref”(不带 S)偏好设置。

您已经拥有公共(public)常量 MyPREFERENCES,也尝试在 MainActivity 中使用它。

关于java - 主要 Activity 未设置主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35415030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com