2016-12-03 21 views
0

隨着搜索條我可以改變背景顏色,但無法保存shanged 的顏色,當開啓的活動如何獲取視圖的背景顏色,將其保存在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

試試這個https://developer.android.com/training/basics/data-storage/shared-preferences.html –

回答

0

使用此class通過您的查看並獲取顏色代碼。那麼你可以用你的colour_Key保存它!

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

你可以這樣做:

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

你可以這樣做,

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();