Итак, у меня есть этот код, и я пытаюсь получить текущий цвет фона 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());
если 'option1' надувается из XML ид' андроида: ид = "@ + идентификатор/option_1" 'то есть' 'Drawable' ресурс drag_drop_option', а не сплошной цвет ... вы сказали в предыдущем комментарии, что у вас есть 'android: background =" # 32cd32 ", но я не вижу эту строку здесь нигде.особенно для «TextViews» – snachmsm
жаль, что это было то, что я пытался. это был андроид: background = "# 32cd32" во время ошибки, и теперь он просто указывает на –
Я не думаю, что это был ... show code, который бросает вы исключение, код excagly. это с линией 'android: background =" @ drawable/drag_drop_option ", вы можете получить один, с' "# 32cd32" 'Я так не думаю ... – snachmsm