简体   繁体   中英

Java code does not work properly - why so?

I am new to Java and Android Studio, I decided to get Spinner to switch the application theme in the settings, but the program does not work correctly.

When you select “Light Theme” in SharedPreferences, “Dark Theme” is saved, when you select “Dark Theme” - “Dark Gray Theme”, “Dark Gray Theme” - “Light Theme”. I need to correct this misunderstanding. Method "ThemeUtils.changeToTheme (Theme);" not implemented.

SettingsActivity

public class SettingsActivity extends AppCompatActivity  {
SharedPreferences colorPref;


public static final String COLOR_PREF = "l";

Spinner ThemeSpinner;
String Theme;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    colorPref = getSharedPreferences(COLOR_PREF, Context.MODE_PRIVATE);

    ThemeSpinner = (Spinner) findViewById(R.id.SpinnerTheme);
    final ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.Themes, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ThemeSpinner.setAdapter(adapter);
    ThemeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
             Theme = (String)adapterView.getItemAtPosition(position 
            colorPref = getPreferences(MODE_PRIVATE);
            boolean hasVisited = colorPref.getBoolean("hasVisited", false); 
            String savedTheme = colorPref.getString(COLOR_PREF, ""); 

            if (!hasVisited) {
                SharedPreferences.Editor e = colorPref.edit();
                String savedTheme_First = getString(R.string.Pink_White);
                savedTheme = savedTheme_First;                e.putString(COLOR_PREF, savedTheme);
                e.putBoolean("hasVisited", true);
                e.apply();
            }

            if(savedTheme != Theme){
                SaveTheme();
                ThemeUtils.changeToTheme(Theme);
                Toast toast = Toast.makeText(getApplicationContext(), "Selecrted : " + savedTheme, Toast.LENGTH_SHORT);
                toast.show();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

}

    public void SaveTheme(){

        colorPref = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor ed = colorPref.edit();
        ed.putString(COLOR_PREF, Theme);
        ed.apply();
    }

Array.xml

<string-array name="Themes">
        <item>@string/Pink_White_rus</item>
        <item>@string/Pink_Dark_rus</item>
        <item>@string/Pink_VeryDark_rus</item>
    </string-array>

strings.xml

<string name="Pink_White">Light Theme</string>
<string name="Pink_Dark">Dark Gray Theme</string>
<string name="Pink_VeryDark">Dark Theme</string>

Spinner positions can be maintained by storing their position value as provided by the OnItemSelectedListener, this can be stored in SharedPreferences and then this can be set as spinner.setSelection() method call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM