1

我在一个活动中为我的搜索栏和切换按钮创建了一个共享首选项,但我不能做的是如何将此首选项应用到其他活动中,因此当我更改搜索栏时,所有活动中的所有文本大小都会更改,并且当我切换按钮时textview 和布局的所有颜色都会在其他活动中发生变化

public class CollectionPrayersTextActivity extends Activity {

// Colors Shared Preferences
public SharedPreferences tprefs;
private ToggleButton toggle;
private LinearLayout linear;
// Text Size Shared Preferences
private SharedPreferences prefs;
public static TextView textview;
private SeekBar seekbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Window window = getWindow();
    // Unlock the device if locked
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    // Turn screen on if off
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    // Keep screen on
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    // Transition between activities
    overridePendingTransition(R.anim.incoming, R.anim.outgoing);
    // On Create
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_collectionprayers_text);
    // Determine The Tools
    seekbar = (SeekBar) findViewById(R.id.seekBarcollectionprayerstext);
    textview = (TextView) findViewById(R.id.id_collectionprayers_txt);
    toggle = (ToggleButton) findViewById(R.id.toggleButton1);
    linear = (LinearLayout) findViewById(R.id.linearcollection);
    // Toogle Share Preferences
    SharedPreferences tprefs = getSharedPreferences(
            "com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE);
    toggle.setChecked(tprefs.getBoolean("Switchable", true));
    if (toggle.isChecked()) {
        textview.setTextColor(Color.WHITE);
        linear.setBackgroundColor(Color.BLACK);
        textview.setShadowLayer(0, 0, 0, Color.WHITE);

    } else {

        textview.setTextColor(Color.BLACK);
        linear.setBackgroundColor(Color.WHITE);
        textview.setShadowLayer(0, 0, 0, Color.BLACK);

    }
    // Get Extra From Another Activity
    Intent n = getIntent();
    String mrng = n.getStringExtra("key");
    textview.setText(Html.fromHtml(mrng));
    // SeekBar Preferences
    prefs = getPreferences(MODE_PRIVATE);
    float fs = prefs.getFloat("fontsize", 40);
    seekbar.setProgress((int) fs);
    textview.setTextSize(TypedValue.COMPLEX_UNIT_PX, seekbar.getProgress());
    // Programming SeekBar
    seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            prefs = getPreferences(MODE_PRIVATE);
            SharedPreferences.Editor ed = prefs.edit();
            ed.putFloat("fontsize", textview.getTextSize());
            ed.commit();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            textview.setTextSize(TypedValue.COMPLEX_UNIT_PX, progress);
        }
    });
    // Programming ToggleButton
    toggle.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (toggle.isChecked()) {
                textview.setTextColor(Color.WHITE);
                linear.setBackgroundColor(Color.BLACK);
                textview.setShadowLayer(0, 0, 0, Color.WHITE);
                SharedPreferences.Editor editor = getSharedPreferences(
                        "com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE)
                        .edit();
                editor.putBoolean("Switchable", true);
                editor.commit();

            } else {

                textview.setTextColor(Color.BLACK);
                linear.setBackgroundColor(Color.WHITE);
                textview.setShadowLayer(0, 0, 0, Color.BLACK);
                SharedPreferences.Editor editor = getSharedPreferences(
                        "com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE)
                        .edit();
                editor.putBoolean("Switchable", false);
                editor.commit();

            }
        }
    });
}

public void c_default(View V) {
    textview.setTextColor(getResources().getColor(R.color.Vanilla));
    linear.setBackgroundColor(getResources().getColor(R.color.Maroon));
    textview.setShadowLayer((float) 1.5, 2, 2, Color.BLACK);
}

@Override
public void onBackPressed() {
    Intent intent_e3tiraf_back = new Intent(
            CollectionPrayersTextActivity.this,
            CollectionPrayersActivity.class);
    startActivity(intent_e3tiraf_back);
    finish();
}
}

有什么帮助吗???

4

3 回答 3

1
SharedPreferences mPref = context.getSharedPreferences(
                "com.e_orthodoxy.orthodox_prayers", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor = mPref.edit();
                Boolean userid=mPref.getBoolean("Switchable", false)
于 2013-09-20T10:34:39.580 回答
1

您可以通过SharedPreference以下方式获取另一个活动。

SharedPreferences tprefs = getSharedPreferences(
        "com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE);
String textSize = tprefs.getString("fontsize",null);

这里 textSize 是您在下一个活动中想要的 textview 的大小。

于 2013-09-20T10:31:06.103 回答
0

第一个问题不清楚。但是如果你按下后退按钮并且所有的都改变了,就像你提到的那样,在 onResume() 中编写 textview 颜色和大小更改代码。这是一个 sherepreference 代码,它从共享首选项中获取值

                    SharedPreferences userGetSession = getSharedPreferences(FRConstants.ACCOUNT_DETAILS_SP, Activity.MODE_PRIVATE);
                    sessionVal = userGetSession.getString("login_session_key", null);
                    userID = userGetSession.getString("login_user_id_key", null);
于 2013-09-20T11:27:09.457 回答