Android通过不同的活动更改背景颜色

问题描述 投票:0回答:2

我正在尝试更改主要活动的背景色。我从菜单中选择设置,然后通过设置进入另一个活动。当我单击复选框更改背景时,它崩溃了。我正在尝试更改主要活动视图的背景。 LinearLayout是主要活动的子项,也是我希望更改的子项。但是我一直在获得nullpointer,如何解决该问题或需要什么才能使它起作用呢?

设置活动类:

public class ShowSettingsActivity extends Activity {
    static final String TAG = "CAT";

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);

        StringBuilder builder = new StringBuilder();

        builder.append("\n" + sharedPrefs.getBoolean("night_mode_key", true));
        builder.append("\n" + sharedPrefs.getString("time_usage_key", "-1"));
        builder.append("\n" + sharedPrefs.getString("language_key", "-1"));

        //builder.append("\n" + sharedPrefs.getBoolean("time_history_key", true));

        TextView settingsTextView = (TextView) findViewById(R.id.settings_text_view);
        settingsTextView.setText(builder.toString());    
    }
}

偏好活动类别:

public class QuickPrefsActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
    CheckBoxPreference isReg;
    static final String TAG = "QuickPrefsActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);        
        addPreferencesFromResource(R.xml.preferences); 

        PreferenceManager.setDefaultValues(this,R.xml.preferences, false);

        CheckBoxPreference  night_checkbox = (CheckBoxPreference) findPreference("night_mode_key");        

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //menu.add(Menu.NONE, 0, 0, "Show current settings");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                startActivity(new Intent(this, ShowSettingsActivity.class));
                return true;
        }
        return false;
    }

    @Override
    public void onStart(){
        super.onStart();
        SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(QuickPrefsActivity.this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        SharedPreferences s = getSharedPreferences("MY_PREFS", 0);

        // Create a editor to edit the preferences:
        SharedPreferences.Editor editor = s.edit();

        // Let's do something a preference value changes
        if (key.equals("night_mode_key")) 
        {
            // Create a reference to the checkbox (in this case):
            CheckBoxPreference mHints = (CheckBoxPreference)getPreferenceScreen().findPreference("night_mode_key");

            //Lets change the summary so the user knows what will happen using a one line IF statement:
            //mHints.setSummary(mHints.isChecked() ? "Hints will popup." : "No hints will popup.");
            // Lets store the new preference:
            //SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            boolean checkBoxValue = s.getBoolean("night_mode_key", false);

            if (checkBoxValue) 
            {
                Log.d(TAG, "true onshared" + checkBoxValue);

                //setContentView(R.layout.activity_main);
                View v = findViewById(R.id.LinearLayout1);
                //View root = v.getRootView();

                v.setBackgroundColor(0xffffffff);
                /*((TextView) findViewById(R.id.hanziTextView)).setTextColor(0xff000000);
                  ((TextView) findViewById(R.id.timerTextView)).setTextColor(0xff000000);
                  ((TextView) findViewById(R.id.instructionTextView)).setTextColor(0xff000000);*/

            }
            else
            {
            /* findViewById(R.id.LinearLayout1).setBackgroundColor(0xff000000);
                ((TextView) findViewById(R.id.hanziTextView)).setTextColor(0xff444444);
                ((TextView) findViewById(R.id.timerTextView)).setTextColor(0xff444444);
                ((TextView) findViewById(R.id.instructionTextView)).setTextColor(0xff444444);*/
                Log.d(TAG, "false onshared" + checkBoxValue);
            }                      

            //checkBox.setChecked(false);

            editor.putBoolean("night_mode_key", mHints.isChecked());

            // Save the results:
            editor.commit();
        }

        if (key.equals("language_key")) {

            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
           //ListPreference listPref = (ListPreference) sharedPreferences; 
           String entryvalue = sharedPreferences.getString( "language_key", "");
           Log.d(TAG, "Entryvalue " + entryvalue);

            if (entryvalue.equals("EN"))
            {
                Log.d(TAG, "EN " + entryvalue);
                Toast.makeText(getBaseContext(), "English Selected", Toast.LENGTH_SHORT).show();
            }
            else if (entryvalue.equals("CH"))
            {
                Log.d(TAG, "CH " + entryvalue);
                Toast.makeText(getBaseContext(), "Chinese Selected", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(getBaseContext(), "You may not go where no1 has gone before, pick again!", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

主布局xml ...我正在尝试更改背景的那个:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">        

    <android.gesture.GestureOverlayView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/gestureOverlayView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        tools:context=".MainActivity">

        <LinearLayout 
            android:id="@+id/LinearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="10dp" 
            android:layout_gravity="start">

            <TextView
                android:id="@+id/timerTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginRight="5sp"
                android:text="@string/startTime"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textSize="30sp" />

        </LinearLayout>

    </android.gesture.GestureOverlayView>

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#666"
        android:dividerHeight="1dp"
        android:background="#333"
        android:paddingLeft="15sp"
        android:paddingRight="15sp"
        />    
</android.support.v4.widget.DrawerLayout>
android android-layout view
2个回答
2
投票

它将崩溃,因为目标LinearLayout在另一个活动中。您可以做的是..将背景色或设置的颜色保存在Sharedpreferences文件中。然后在主活动中再次调用它以更改颜色...

 if (checkBoxValue) 
         {
            SharedPreferences sp = getSharedPreferences("settings", Context.MODE_PRIVATE);
            Editor edit = sp.edit();
            edit.putString("COLOR", "0xffffffff")
         }

然后在您的主检查中,sharedPreferences的值...

...
SharedPreferences sp = getSharedPreferences("settings", Context.MODE_PRIVATE);
v.setBackgroundColor(sp.getString("COLOR"), Color.WHITE); // here check if the value available, if not put the color to default.. maybe white?...

这是未经测试的代码。我粗略地写了它。但是我希望你能理解。请给我反馈


0
投票

您无法从另一个活动访问主活动的布局视图。这样做的方法是在意图中添加额外的东西以再次启动主活动,或者可以使用SharedPreferences。

© www.soinside.com 2019 - 2024. All rights reserved.