2013-01-24 3 views

risposta

18

Usa

Drawable[] drawables = textView.getCompoundDrawables(); 

Inoltre, se si desidera confrontare due drawable.

Bitmap bitmap = ((BitmapDrawable)drawables[1]).getBitmap(); 
Bitmap bitmap2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.twt_hover)).getBitmap(); 

if(bitmap == bitmap2) 
    { 
     //Code blcok 
    } 
+2

Non penso che sia un buon modo per fare in modo che l'app crei due bitmap e li confronti per ogni clic su un TextView, giusto? –

+0

probabilmente giusto, ma ho trovato questa risposta su questo thread http://stackoverflow.com/questions/9125229/comparing-two-drawables-in-android –

+6

getCompoundDrawables() li restituisce nell'ordine specificato Left, Top, Right, Bottom – defhlt

2

Penso che dovresti usare textView.getCompoundDrawables(); Per quanto riguarda il drawable top, penso che sarebbe il secondo drawable ad esempio

Drawables tmp[] = textView.getCompoundDrawables(); 
tmp[1]; //this should be your top drawable but not 100% sure. 
+0

hai cambiato la domanda) – Leonidos

+0

ho cambiato la domanda? – Raigex

2

Non è possibile ottenere ID disegnabile in runtime. TextView utilizza ID solo nel suo costruttore per caricare il corrispondente drawable. Dopo che il drawable è stato caricato, l'ID è scomparso.

In alcuni casi non esiste alcun ID estraibile. drawableTop può essere un valore rgb di colore.

5

In primo luogo, dare il vostro TextView un ID in modo da poter trovare nel vostro Activity:

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dip" 
    android:drawableTop="@drawable/new" /> 

In secondo luogo, nel vostro Activity trovare il tuo TextView e ottenere le drawable composti nel metodo onCreate dopo aver chiamato setContentView:

TextView textView = (TextView) findViewById(R.id.textView); 
// Left, top, right, bottom drawables. 
Drawable[] compundDrawables = textView.getCompoundDrawables(); 
// This is the top drawable. 
Drawable topCompoundDrawable = compoundDrawables[1];