4

我有一个填充了 textView 元素的 gridView 布局。我有一个用于 gridView 的 onTouch() 实现,其中被触及的单元格需要更改背景颜色。下面是我的代码,但它不起作用,当我触摸时 VM 会关闭。

gridView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent me) {

            int action = me.getActionMasked();
            float currentXPosition = me.getX();
            float currentYPosition = me.getY();
            int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);

            // Change the color of the key pressed
            ((TextView) gridView.getItemAtPosition(position)).setBackgroundColor(Color.RED);
            return true;
    }
}

编辑:Logcat 跟踪

12-10 15:34:28.081: E/InputEventReceiver(829): Exception dispatching input event.
12-10 15:34:28.081: E/MessageQueue-JNI(829): Exception in MessageQueue callback: handleReceiveCallback
12-10 15:34:28.221: E/MessageQueue-JNI(829): java.lang.ClassCastException: java.lang.String cannot be cast to android.widget.TextView
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.example.helloworld.GridViewActivity$1.onTouch(GridViewActivity.java:62)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.View.dispatchTouchEvent(View.java:7122)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.View.dispatchPointerEvent(View.java:7307)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.os.MessageQueue.nativePollOnce(Native Method)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.os.MessageQueue.next(MessageQueue.java:125)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.os.Looper.loop(Looper.java:124)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at java.lang.reflect.Method.invokeNative(Native Method)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at java.lang.reflect.Method.invoke(Method.java:511)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-10 15:34:28.221: E/MessageQueue-JNI(829):    at dalvik.system.NativeStart.main(Native Method)
12-10 15:34:28.263: D/AndroidRuntime(829): Shutting down VM
12-10 15:34:28.263: W/dalvikvm(829): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-10 15:34:28.362: E/AndroidRuntime(829): FATAL EXCEPTION: main
12-10 15:34:28.362: E/AndroidRuntime(829): java.lang.ClassCastException: java.lang.String cannot be cast to android.widget.TextView
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.example.helloworld.GridViewActivity$1.onTouch(GridViewActivity.java:62)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.View.dispatchTouchEvent(View.java:7122)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.View.dispatchPointerEvent(View.java:7307)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.os.MessageQueue.nativePollOnce(Native Method)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.os.MessageQueue.next(MessageQueue.java:125)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.os.Looper.loop(Looper.java:124)
12-10 15:34:28.362: E/AndroidRuntime(829):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-10 15:34:28.362: E/AndroidRuntime(829):  at java.lang.reflect.Method.invokeNative(Native Method)
12-10 15:34:28.362: E/AndroidRuntime(829):  at java.lang.reflect.Method.invoke(Method.java:511)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-10 15:34:28.362: E/AndroidRuntime(829):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-10 15:34:28.362: E/AndroidRuntime(829):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

其实,我想通了!

我在这里对错误的返回类型进行了类型转换

((TextView) gridView.getItemAtPosition(position)).setBackgroundColor(Color.RED);

我真正需要的是方法 getChildAt 而不是 getItemAtPosition (它返回一个字符串)

String s = (String) gridView.getItemAtPosition(position);
TextView tv = (TextView) gridView.getChildAt(position);
tv.setBackgroundColor(Color.RED);
于 2012-12-10T22:15:46.987 回答
0

改为使用gridView.setOnItemClickListener。您还将获得onItemClick方法中的位置,而不必像现在这样计算它。

编辑 我刚刚意识到您想在仍按下该项目时更改颜色。在这种情况下,我建议将适配器中项目的背景设置为可绘制的选择器

于 2012-12-10T21:05:27.593 回答