2016-12-03 2 views
0

Mit seekbar speichern kann ich Hintergrundfarbe ändern, kann aber nicht Farbe, wenn sie offen Aktivität, wie die Hintergrundfarbe einer Ansicht, um es in SharedPreferences

button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        SharedPreferences sharedPref = getSharedPreferences("COLOR", MODE_PRIVATE); 
        SharedPreferences.Editor editor = sharedPref.edit(); 
        editor.putInt("color_KEY", ); //?? what code for this? 
        editor.commit(); 
       } 
      }); 
     } 
     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
      mScreen.setBackgroundColor(Color.rgb(red.getProgress(), green.getProgress(),blue.getProgress())); 
     } 
     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) { 
     } 
     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) { 
     } 
    } 
+0

editor.putInt ("color_KEY", button.getSolidColor()); –

+0

versuchen Sie dies https://developer.android.com/training/basics/data-storage/shared-preferences.html –

Antwort

0

verwenden class passieren shanged speichern Sie Ihre Sehen Sie sich an und erhalten Sie den Farbcode. dann kannst du es mit deinem colour_Key speichern wie du willst!

private Bitmap mBitmap; 
    private Canvas mCanvas; 
    private Rect mBounds; 

    public void initIfNeeded() { 
     if(mBitmap == null) { 
     mBitmap = Bitmap.createBitmap(1,1, Bitmap.Config.ARGB_8888); 
     mCanvas = new Canvas(mBitmap); 
     mBounds = new Rect(); 
     } 
    } 

    public int getBackgroundColor(View view) { 
     // The actual color, not the id. 
     int color = Color.BLACK; 

     if(view.getBackground() instanceof ColorDrawable) { 
     if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { 
      initIfNeeded(); 

      // If the ColorDrawable makes use of its bounds in the draw method, 
      // we may not be able to get the color we want. This is not the usual 
      // case before Ice Cream Sandwich (4.0.1 r1). 
      // Yet, we change the bounds temporarily, just to be sure that we are 
      // successful. 
      ColorDrawable colorDrawable = (ColorDrawable)view.getBackground(); 

      mBounds.set(colorDrawable.getBounds()); // Save the original bounds. 
      colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds. 

      colorDrawable.draw(mCanvas); 
      color = mBitmap.getPixel(0, 0); 

      colorDrawable.setBounds(mBounds); // Restore the original bounds. 
     } 
     else { 
      color = ((ColorDrawable)view.getBackground()).getColor(); 
     } 
     } 

     return color; 
    } 
0

Sie können dies tun:

SharedPreferences sharedPref = getSharedPreferences("COLOR", MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedPref.edit(); 
// Only if you know it is a color 
ColorDrawable buttonColor = (ColorDrawable) button.getBackground(); 
int colorId = buttonColor.getColor(); 
editor.putInt("color_KEY", colorId ); 
editor.commit(); 
0

Sie auf diese Weise tun können,

ColorDrawable buttonColor =(ColorDrawable)button.getBackground(); int colorId = buttonColor.getColor(); SharedPreferences sharedPref = getSharedPreferences("COLOR",MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt("color_KEY",id.toString()); editor.commit();

Verwandte Themen