操作栏中的复选框活动,以改变另一个活动中的textView的颜色



在我的欢迎页面上,有一个带有首选项的操作栏。当单击时,它将用户带到另一个带有三个选项的活动,这些选项都有复选框。当一个或两个复选框被按下时,当用户返回到欢迎页面时,textView的颜色应该随着选择而改变。我相信我已经编码复选框正确,但我无法做出改变,以反映在TextView。怎么解呢?

这是我的欢迎课,

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.TextView;
import android.preference.PreferenceManager;
import android.graphics.Color;

public class Welcome extends Activity {
TextView textView;
SharedPreferences sharedPreferences;
public static final String NAME="UserName";
String defaultName = "";
SharedPreferences.Editor editor;
SharedPreferences mypreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
    textView=(TextView)findViewById(R.id.textView);
    sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
    textView.setText("Welcome," + sharedPreferences.getString(NAME, defaultName));
    editor = sharedPreferences.edit();
    editor.putString(NAME, sharedPreferences.getString(NAME,defaultName));
    editor.apply();
}
@Override
protected void onResume() {
    super.onResume();
    boolean checkBox;
    boolean checkBox2;
    boolean checkBox3;
    mypreferences =   PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    checkBox = mypreferences.getBoolean("checkBox", false);
    checkBox2 = mypreferences.getBoolean("checkBox2", false);
    checkBox3 = mypreferences.getBoolean("checkBox3", false);
    if (checkBox && !checkBox2 && !checkBox3) {
        textView.setTextColor(Color.RED);
    } else if (!checkBox && checkBox2 && !checkBox3) {
        textView.setTextColor(Color.GREEN);
    } else if (!checkBox && !checkBox2 && checkBox3) {
        textView.setTextColor(Color.BLUE);
    } else if (checkBox && checkBox2 && !checkBox3) {
        textView.setTextColor(Color.YELLOW);
    } else if (checkBox && !checkBox2 && checkBox3) {
        textView.setTextColor(Color.MAGENTA);
    }

}
    @Override
   public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(Welcome.this, MainActivity.class));
    return;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main,menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_preferences){
        startActivity(new Intent(Welcome.this,Preferences.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

,这是我的首选项页面,其中包含所有复选框的代码。

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.preference.PreferenceManager;
public class Preferences extends Activity {
CheckBox checkBox;
CheckBox checkBox2;
CheckBox checkBox3;
TextView textView;
SharedPreferences mypreferences;
SharedPreferences.Editor myeditor;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.preferences);
    textView=(TextView)findViewById(R.id.textView);
    checkBox=(CheckBox)findViewById(R.id.checkBox);
    checkBox2=(CheckBox)findViewById(R.id.checkBox2);
    checkBox3=(CheckBox)findViewById(R.id.checkBox3);
    mypreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    myeditor= mypreferences.edit();
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                myeditor.putBoolean("checkBox", true);
            } else {
                myeditor.putBoolean("checkBox", false);
            }
            myeditor.apply();
        }
    });
    checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                myeditor.putBoolean("checkBox2", true);
            } else {
                myeditor.putBoolean("checkBox2", false);
            }
            myeditor.apply();
        }
    });
    checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                myeditor.putBoolean("checkBox3", true);
            } else {
                myeditor.putBoolean("checkBox3", false);
            }
            myeditor.apply();
        }
    });
}
}

我需要在首选项页面中的复选框来更改欢迎页面上的textView。请帮助!

Welcome.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:textSize="30dp" />

Preferences.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:minHeight="80dp"
        android:weightSum="1">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:minWidth="200dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Red Option"
                android:id="@+id/textView2"
                android:textSize="30dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="This is red option"
                android:id="@+id/textView3"
                android:textSize="20dp" />
        </LinearLayout>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/checkBox"
            android:checked="false"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="100dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:minHeight="80dp">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:minWidth="200dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Green Option"
                android:id="@+id/textView4"
                android:textSize="30dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="This is green option"
                android:id="@+id/textView5"
                android:textSize="20dp" />
        </LinearLayout>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/checkBox2"
            android:checked="false"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="20dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:minHeight="80dp">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:minWidth="200dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Blue Option"
                android:id="@+id/textView6"
                android:textSize="30dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="This is blue option"
                android:id="@+id/textView7"
                android:textSize="20dp" />
        </LinearLayout>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/checkBox3"
            android:checked="false"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="20dp" />
    </LinearLayout>
</LinearLayout>

您应该这样做。保存复选框的值在SharedPreferences中,然后使用这些值来设置文本视图的颜色。

Welcome.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.preference.PreferenceManager;
import android.graphics.Color;
public class Welcome extends Activity {
    TextView textView;
    SharedPreferences sharedPreferences;
    public static final String NAME = "UserName";
    String defaultName = "";
    SharedPreferences myPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        textView = (TextView) findViewById(R.id.textView);
        sharedPreferences = getSharedPreferences(NAME, Context.MODE_PRIVATE);
        myPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        textView.setText("Welcome," + sharedPreferences.getString(NAME, defaultName));
    }
    @Override
    protected void onResume() {
        super.onResume();
        boolean checkBox1Value = myPreferences.getBoolean("checkBox1", false);
        boolean checkBox2Value = myPreferences.getBoolean("checkBox2", false);
        boolean checkBox3Value = myPreferences.getBoolean("checkBox3", false);
        //Be ware here that there are other conditions that are not mentioned like for
        //example "checkBox1Value && checkBox2Value && checkBox3Value" and 3 other conditions
        //too (Total 4 unmentioned). If any conditions are not mentioned here and you set these
        //unmentioned conditions from your preferences, your TextView color will not change. So
        //you should also mention these 4 conditions.
        if (checkBox1Value && !checkBox2Value && !checkBox3Value) {
            textView.setTextColor(Color.RED);
        } else if (!checkBox1Value && checkBox2Value && !checkBox3Value) {
            textView.setTextColor(Color.GREEN);
        } else if (!checkBox1Value && !checkBox2Value && checkBox3Value) {
            textView.setTextColor(Color.BLUE);
        } else if (checkBox1Value && checkBox2Value && !checkBox3Value) {
            textView.setTextColor(Color.YELLOW);
        } else if (checkBox1Value && !checkBox2Value && checkBox3Value) {
            textView.setTextColor(Color.MAGENTA);
        }
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(Welcome.this, MainActivity.class));
        finish();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_preferences){
            startActivity(new Intent(Welcome.this, Preferences.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Preferences.java

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.preference.PreferenceManager;
public class Preferences extends Activity {
    CheckBox checkBox1;
    CheckBox checkBox2;
    CheckBox checkBox3;
    SharedPreferences myPreferences;
    SharedPreferences.Editor myEditor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
        checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
        myPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        myEditor = myPreferences.edit();
        checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    myEditor.putBoolean("checkBox1", true);
                } else {
                    myEditor.putBoolean("checkBox1", false);
                }
                myEditor.apply();
            }
        });
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    myEditor.putBoolean("checkBox2", true);
                } else {
                    myEditor.putBoolean("checkBox2", false);
                }
                myEditor.apply();
            }
        });
        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    myEditor.putBoolean("checkBox3", true);
                } else {
                    myEditor.putBoolean("checkBox3", false);
                }
                myEditor.apply();
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        boolean checkBox1Value = myPreferences.getBoolean("checkBox1", false);
        boolean checkBox2Value = myPreferences.getBoolean("checkBox2", false);
        boolean checkBox3Value = myPreferences.getBoolean("checkBox3", false);
        checkBox1.setChecked(checkBox1Value);
        checkBox2.setChecked(checkBox2Value);
        checkBox3.setChecked(checkBox3Value);
    }
}

welcome.xml

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:id="@+id/textView"
    android:textSize="30dp" />

preferences.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:minHeight="80dp"
        android:weightSum="1">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:minWidth="200dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Red Option"
                android:id="@+id/textView2"
                android:textSize="30dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="This is red option"
                android:id="@+id/textView3"
                android:textSize="20dp" />
        </LinearLayout>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/checkBox1"
            android:checked="false"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="100dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:minHeight="80dp">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:minWidth="200dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Green Option"
                android:id="@+id/textView4"
                android:textSize="30dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="This is green option"
                android:id="@+id/textView5"
                android:textSize="20dp" />
        </LinearLayout>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/checkBox2"
            android:checked="false"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="20dp" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:minHeight="80dp">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:minWidth="200dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Blue Option"
                android:id="@+id/textView6"
                android:textSize="30dp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="This is blue option"
                android:id="@+id/textView7"
                android:textSize="20dp" />
        </LinearLayout>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/checkBox3"
            android:checked="false"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="20dp" />
    </LinearLayout>
</LinearLayout>

我已经修复了你所有的代码。你现在需要做的最重要的事情是,当我在上面的活动中评论到的其他4个未提及的条件被设置时,你要做什么。

你犯了一个简单的错误,那就是你忘记设置textView为一个真正的textView如果你检查一下代码你会发现你定义了checkBox checkBox2和checkbox3并给它们指定了Id而你忘了给textView指定你想要改变颜色的Id

最新更新