如何在滑动时获取共享首选项以保存背景颜色

时间:2017-04-30 03:51:31

标签: android sharedpreferences

我正在学习共享首选项以及如何在关闭应用程序时保存背景颜色。当我按下按钮来改变颜色时我有工作但是当我向左滑动以使颜色变亮并使颜色变暗时我也试图保存颜色。当我退出并再次打开应用程序时,它会回到按下最后一个按钮时的颜色,而不是滑动。我需要更改什么才能保存?我是否需要添加一个新的侦听器来检查滑动中的更改?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("TurnToTech", "Project Name - SharedPreference");


        lib = GestureLibraries.fromRawResource(MainActivity.this,
                R.id.gestureOverlayView1);
        gesture = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
        gesture.addOnGesturePerformedListener(this);

        radioColorGroup = (RadioGroup) findViewById(R.id.radioColor);
        radioColorGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            // Called when the checked state of a compound button has changed.
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int selectedId = radioColorGroup.getCheckedRadioButtonId();
                radioColorButton = (RadioButton) findViewById(selectedId);


                SharedPreferences settings = getSharedPreferences(prefs_name,0);

                SharedPreferences.Editor editor = settings.edit();

                editor.putString("col",radioColorButton.getText().toString());

                editor.commit();
                setBackground();

            }
        });

        setBackground();


    } 

@Override
    protected  void onPause(){
        super.onPause();

        SharedPreferences settings = getSharedPreferences(prefs_name, 0);
        SharedPreferences.Editor editor = settings.edit();

        editor.putString("col", CurrentColor);

        editor.apply();
        setBackground();
    }

 @Override
    public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {

        SharedPreferences settings = getSharedPreferences(prefs_name, 0);

        ArrayList<GestureStroke> strokeList = gesture.getStrokes();

        float f[] = strokeList.get(0).points;
        String str = "";

        if (f[0] < f[f.length - 2]) {
            str = "Right gesture";
        } else if (f[0] > f[f.length - 2]) {
            str = "Left gesture";
        } else {
            str = "no direction";
        }


        if(str =="Left gesture" && (CurrentColor == "Green" || CurrentColor == "Dark Green") ) {
            radioColorGroup.setBackgroundColor(Color.parseColor("#19faa4"));
            CurrentColor = "Bright Green";
        }

        else if (str == "Right gesture" && (CurrentColor == "Green" || CurrentColor == "Bright Green")) {
            radioColorGroup.setBackgroundColor(Color.parseColor("#004e37"));
            CurrentColor = "Dark Green";
        }

        else if(str =="Left gesture" && (CurrentColor == "Red" || CurrentColor == "Dark Red")) {
            radioColorGroup.setBackgroundColor(Color.parseColor("#f04242"));
            CurrentColor = "Bright Red";
        }

        else if (str == "Right gesture" && (CurrentColor == "Red" || CurrentColor == "Bright Red")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#84001b"));
            CurrentColor = "Dark Red";
        }

        else if(str =="Left gesture" && (CurrentColor == "Blue" || CurrentColor == "Dark Blue")) {
            radioColorGroup.setBackgroundColor(Color.parseColor("#54dde9"));
            CurrentColor = "Bright Blue";
        }

        else if (str == "Right gesture" && (CurrentColor == "Blue" || CurrentColor == "Bright Blue")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#000080"));
            CurrentColor = "Dark Blue";
        }

    }


    //change the background
    private void setBackground(){

        SharedPreferences settings = getSharedPreferences(prefs_name, 0);

        // Retrieve a String value from the preferences. default "Green"
        String color_pref = settings.getString("col", "Blue");

        // After retrieving change background color accordingly.
        if(color_pref.equals("Green")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#19a57c"));
            radioColorGroup.check(R.id.radioGreen);
            CurrentColor = "Green";
        }
        if(color_pref.equals("Red")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#dd002e"));
            radioColorGroup.check(R.id.radioRed);
            CurrentColor = "Red";
        }
        if(color_pref.equals("Blue")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#325eff"));
            radioColorGroup.check(R.id.radioBlue);
            CurrentColor = "Blue";
        }

        if(color_pref.equals("Bright Green")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#19faa4"));
            radioColorGroup.check(R.id.radioGreen);
            CurrentColor = "Bright Green";
        }
        if(color_pref.equals("Bright Red")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#f04242"));
            radioColorGroup.check(R.id.radioRed);
            CurrentColor = "Bright Red";
        }
        if(color_pref.equals("Bright Blue")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#54dde9"));
            radioColorGroup.check(R.id.radioBlue);
            CurrentColor = "Bright Blue";
        }

        if(color_pref.equals("Dark Green")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#004e37"));
            radioColorGroup.check(R.id.radioGreen);
            CurrentColor = "Dark Green";
        }
        if(color_pref.equals("Dark Red")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#84001b"));
            radioColorGroup.check(R.id.radioRed);
            CurrentColor = "Dark Red";
        }
        if(color_pref.equals("Dark Blue")){
            radioColorGroup.setBackgroundColor(Color.parseColor("#000080"));
            radioColorGroup.check(R.id.radioBlue);
            CurrentColor = "Dark Blue";
        }
    }

0 个答案:

没有答案