操作栏中的复选框活动可更改另一个活动中textView的颜色

时间:2015-08-19 22:42:10

标签: android android-activity checkbox colors 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>

2 个答案:

答案 0 :(得分:1)

这是你应该怎么做的。将复选框值保存在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个未提及的条件时该怎么做。

答案 1 :(得分:0)

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