2011-11-11 83 views
33

ボタンの背景色を取得する方法を教えてください。 xmlでは、---- android:background = XXXXX アクティビティクラス内の背景色を設定しました。この値はどのように取得できますか?androidのボタンの背景色を取得する

答えて

73

残念ながら、実際の色を取得する方法はわかりません。

それはあなたが、これは、あなたが試すことができ

ColorDrawable buttonColor = (ColorDrawable) button.getBackground(); 

色であることがわかっている場合Drawable

Button button = (Button) findViewById(R.id.my_button); 
Drawable buttonBackground = button.getBackground(); 

としてこれを取得するのは簡単ですそして、あなたは、Androidにしている場合は、抜け出すことができ3.0+色のリソースID

int colorId = buttonColor.getColor(); 

これをあなたの割り当てられた色と比較してください。

if (colorID == R.color.green) { 
    log("color is green"); 
} 
+2

あなたはGETCOLOR()は、IDを取得しますか?私はそれが色の実際のint値を取得すると思う(すなわち0xAARRGGBB)。私はこれを "#00000001"でテストし、1を返しました。 – brianestey

+1

私のテストでは、 'button.setBackground(R.color.green)'を実行し、応答をチェックしました。私は 'Color.red(int)'、 'Color.green(int)'、 'Color.blue(int)'とすることができるように、適切な色整数を好むでしょう。しかし、Android 3.2での私のテストでは、そうではありませんでした。私はそれが矛盾していると推測し、文脈に応じて、色int、または残余を返します。 –

+1

それはAPI 11+ –

2

背景Drawableを取得するには基本Viewクラスで定義されている、あなたは

public Drawable getBackground(); 

を使用しています。

Buttonは、背景、画像、色、グラデーションを持つことができます。あなたがアンドロイドを使用する場合:背景= "#FFFFFFのを"、背景のクラスが

android.graphics.drawable.ColorDrawableが

そこから、あなたは、単に呼び出すことができるでしょう

public int getColor() 
14

また、取得するための最も単純な方法

android:tag="#ff0000" 

とコード

String colorCode = (String)btn.getTag(); 
19
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; 
} 
+3

これは受け入れられた答えでなければなりません – IHeartAndroid

5

からアクセスするようなタグとして色の値を設定するような何かを試すことができます私の色は:

int color = ((ColorDrawable)button.getBackground()).getColor(); 
ですテスト済み

、これを試してみてくださいロリポップ5.1.1

0

に取り組んで:

list_view.getChildAt(position).setBackgroundColor(Color.YELLOW);   
ColorDrawable corItem = (ColorDrawable) list_view.getChildAt(position).getBackground(); 
if(corItem.getColor() == Color.YELLOW){ 
    Toast.makeText(NovoProcessoActivity.this,"Right Color!", Toast.LENGTH_SHORT).show(); 
    }else{ 
    Toast.makeText(NovoProcessoActivity.this,"Wrong Color!", Toast.LENGTH_SHORT).show(); 
} 

または

int color =((ColorDrawable) list_view.getChildAt(position).getBackground()).getColor(); 
関連する問題