2017-02-15 82 views
0

所以我有这个代码,我试图获得已被点击的textView的当前背景颜色。获取textView的背景颜色(我不知道发生了什么)

我得到以下错误:

java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ColorDrawable

private final class ChoiceTouchListener implements OnTouchListener { 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 

      TextView textViewTouched = (TextView) view; 

***error at this line*** ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground(); 


      //change color of textView quickly so that the shadow dragged object made down below 
      //will have a different colour then textView 
      view.setBackgroundColor(0xfff00000); 

      //setup drag 
      ClipData data = ClipData.newPlainText("", ""); 
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      //start dragging the item touched 
      view.startDrag(data, shadowBuilder, view, 0); 

      //change the textView colour back so the user doesn't see a change so that the 
      //shadow object is a different colour then the textView 
      view.setBackgroundColor(0xf32cd320); 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 
} 

但是,如果我移动

ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground(); 

要低于

view.setBackgroundColor(0xfff00000); 

它工作得很好。

我不知道为什么它在做什么,但我需要在应用新颜色之前获取背景颜色。

编辑

我还要提到我有这样的功能在我的代码低了下去,并在该函数工作得很好。

更多编辑

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/DisplayConfig" 
    android:keepScreenOn="true"> 


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp"> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="18.5dp" 
     android:gravity="center" 
     android:paddingBottom="0dp" 
     android:text="@string/intro" /> 

    <TextView 
     android:id="@+id/option_1" 
     android:layout_width="fill_parent" 
     android:layout_margin="0dp" 
     android:background="@drawable/drag_drop_option" 
     android:gravity="left" 
     android:text="@string/option_1" 
     android:textStyle="normal" 
     android:textColor="#FFFFFF" 
     android:layout_height="150dp" /> 

    <TextView 
     android:id="@+id/option_2" 
     android:layout_width="fill_parent" 
     android:layout_margin="0dp" 
     android:background="@drawable/drag_drop_option" 
     android:gravity="left" 
     android:text="@string/option_2" 
     android:textColor="#FFFFFF" 
     android:textStyle="normal" 
     android:layout_height="150dp" /> 

    <TextView 
     android:id="@+id/option_3" 
     android:layout_width="fill_parent" 
     android:layout_margin="0dp" 
     android:background="@drawable/drag_drop_option" 
     android:gravity="left" 
     android:text="@string/option_3" 
     android:textColor="#FFFFFF" 
     android:textStyle="normal" 
     android:layout_height="151dp" /> 


    </LinearLayout> 

    <LinearLayout android:id="@+id/bottom_button_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|center_horizontal" 
    android:background="#000000" 
    android:orientation="horizontal"> 

    <Button android:id="@+id/occupancy_button" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="wrap_content" 
     android:layout_height="72dp" 
     android:layout_weight="1" 
     android:text="Room Mode" 
     android:textColor="#FFFFFF"/> 

    </LinearLayout> 
</FrameLayout> 

和听众的观点的设置像这样做。

option1.setOnTouchListener(new ChoiceTouchListener()); 
+0

:ID = “@ + ID/option_1”'那么它有'Drawable'资源'drag_drop_option',不是纯色......你在prev评论中说过你有'android:background =“#32cd32”'set,但是我没有在这里看到这行。特别是对于'TextViews' – snachmsm

+0

抱歉,这是我正在尝试。它是android:background =“#32cd32”在错误的时候,现在它只是指向

+0

我不认为它是...显示代码,它会抛出你是个例外,代码很粗糙。这与行'android:background =“@ drawable/drag_drop_option”'你可能会得到一个,''#32cd32“'我不这么认为...... – snachmsm

回答

0

也许你没有ColorDrawable但另一种类型,所以你得到ClassCast

HEREDrawable有很多子类

Known Direct Subclasses

AnimatedVectorDrawable, AnimatedVectorDrawableCompat, BitmapDrawable, ColorDrawable, DrawableContainer, DrawableWrapper, DrawerArrowDrawable, GradientDrawable, LayerDrawable, NinePatchDrawable, PictureDrawable, RoundedBitmapDrawable, ShapeDrawable, VectorDrawable, VectorDrawableCompat

Known Indirect Subclasses

AnimatedStateListDrawable, AnimationDrawable, ClipDrawable, InsetDrawable, LevelListDrawable, PaintDrawable, RippleDrawable, RotateDrawable, ScaleDrawable, StateListDrawable, TransitionDrawable

编辑:

getBackground() method回报Drawable和你immediatelly铸造这(ColorDrawable)。堆栈跟踪说,这是不是ColorDrawable, but GradientDrawableDrawable

setBackgroundColor(int color)另一个子创造ColorDrawable背景,所以,当你移动你的线下与铸造ColorDrawable则工作

可能不抛出任何错误其他意见,因为这些可能已经设置背景颜色早些时候在另一种方式

EDIT2:

您可以用instanceOf检查,但这不是一个有效的和最佳的方式如果`option1`从XML ID'的Android充气解决您的问题...

if(! textViewTouched.getBackground() instanceOf ColorDrawable) 
    textViewTouched.setBackgroundColor(Color.parseColor("#32cd32")); 
ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground(); 
+0

有**导入android.graphics.drawable.ColorDrawable; **但也尝试过**导入android.graphics.drawable。*; ** –

+0

进口是自动的,在这里根本没有关系...查看编辑 – snachmsm

+0

是还有另一种方法,我可以创建一个ColorDrawable出来设置颜色? –