使用设置以编程方式更改android活动背景颜色

时间:2018-10-07 06:12:20

标签: java android

因此,我一直在尝试使用设置在代码本身内更改活动的背景。我知道我需要在onCreate()方法中声明我的视图,但是我可以在我的静态类中访问它。

即使在View appView类中全局声明了变量,我也无法访问sBindPreferenceSummaryToValueListener中的Settings变量。

我试图做的是,根据ListPreference的选定值,活动的背景颜色将改变。

我将不胜感激!

我的代码在下面:

public class Settings extends AppCompatPreferenceActivity {

    View appView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // To change background programmatically
        appView = (View) findViewById(R.id.main_menu_layout);

        // load settings fragment
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
    }

    public static class MainPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_main);

            // gallery EditText change listener
            //bindPreferenceSummaryToValue(findPreference(getString(R.string.key_name)));

            // notification preference change listener
            //bindPreferenceSummaryToValue(findPreference(getString(R.string.key_notifications_new_message_ringtone)));

        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    private static void bindPreferenceSummaryToValue(Preference preference) {
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }

    /**
     * A preference value change listener that updates the preference's summary
     * to reflect its new value.
     */
    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            String stringValue = newValue.toString();

            if (preference instanceof ListPreference) {
                // For list preferences, look up the correct display value in
                // the preference's 'entries' list.
                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);

                // Set the summary to reflect the new value.
                preference.setSummary(
                        index >= 0
                                ? listPreference.getEntries()[index]
                                : null);

                //View view = (View) findViewById(R.id.main_menu_layout);
                if (index == 1){
                    appView.setBackgroundColor(Color.RED);
                }

            }  else if (preference instanceof EditTextPreference) {
                if (preference.getKey().equals("key_name")) {
                    // update the changed gallery name to summary filed
                    preference.setSummary(stringValue);
                }
            } else {
                preference.setSummary(stringValue);
            }
            return true;
        }
    };

}

1 个答案:

答案 0 :(得分:-1)

您不能静态引用非静态字段。可以将方法更改为非静态类方法,或者尝试将您正在使用的设置的实例传递给该方法。或将其传递给视图对象。