所以我有这个代码,我试图获得已被点击的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());
:ID = “@ + ID/option_1”'那么它有'Drawable'资源'drag_drop_option',不是纯色......你在prev评论中说过你有'android:background =“#32cd32”'set,但是我没有在这里看到这行。特别是对于'TextViews' – snachmsm
抱歉,这是我正在尝试。它是android:background =“#32cd32”在错误的时候,现在它只是指向 –
我不认为它是...显示代码,它会抛出你是个例外,代码很粗糙。这与行'android:background =“@ drawable/drag_drop_option”'你可能会得到一个,''#32cd32“'我不这么认为...... – snachmsm