如何保存切换按钮状态使用共享preferences

如何保存切换按钮状态使用共享preferences

本文介绍了如何保存切换按钮状态使用共享preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让这个切换按钮的状态保存和使用共享preferences在我所有的活动中,我把共享preferences code,但是这一次不工作,所以是不是我错过或本code是错

how to let this toggle button state to be saved and used in all my activities using shared preferences, I put the shared preferences code but this one don't work so is there something i missed or this code is fault

这是新的code和全班code,请检查一下,也许什么效果code

this is the new code and the whole class code please check it, maybe something effect the code

public class CollectionPrayersTextActivity extends Activity {

boolean on;
public SharedPreferences tprefs;
final String PREF_NAME="preferences";
public static TextView textview;
private SharedPreferences prefs;
private SeekBar seekbar;
private ToggleButton toggle;
private LinearLayout linear;

@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.linear1);
    // Toogle Share Preferences

    SharedPreferences tprefs = getSharedPreferences("com.e_orthodoxy.orthodox_prayers", MODE_PRIVATE);
    toggle.setChecked(tprefs.getBoolean("On", true));
    // 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("On", 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("Off", 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();
}

}

推荐答案

要保存:

@Override
public void onClick(View v)
{
    if (toggle.isChecked())
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
        editor.putBoolean("NameOfThingToSave", true);
        editor.commit();
    }
    else
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
        editor.putBoolean("NameOfThingToSave", false);
        editor.commit();
    }
}

要负载:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyle", MODE_PRIVATE);
    toggle.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true));
}

<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.Editor.html#putBoolean%28java.lang.String,%20boolean%29">Shared$p$pferences.Editor.putBoolean

<一个href="http://developer.android.com/reference/android/content/Shared$p$pferences.html#getBoolean%28java.lang.String,%20boolean%29">Shared$p$pferences.Editor.getBoolean

这篇关于如何保存切换按钮状态使用共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:24