在触摸时更改textview的背景颜色

时间:2014-04-01 15:41:56

标签: android ontouchlistener

我必须模拟列表视图,因为我有一个非常冗长的布局,我在使用scrollview。为了模拟列表视图,我使用的是具有垂直方向的线性布局,包含以下布局多次

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/txt_text_view_directclass"
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="wrap_content" />
        <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray"/>
</LinearLayout>

我希望在每个项目上模拟ontouch事件,以便在用户触摸时更改每个项目的背景颜色。此外,我需要为每个项目

设置点击事件

以下代码无效。

final View rowView = getActivity().getLayoutInflater().inflate(R.layout.text_view_directclass, null);

                 rowView.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch(event.getAction()) {
                           case MotionEvent.ACTION_DOWN:
                                v.setBackgroundColor(Color.BLUE);
                                break;
                           case MotionEvent.ACTION_UP:
                                v.setBackgroundColor(Color.BLACK);
                                break;
                       }
                       return true;
                    }
                });

正确切换动作上的颜色并降低动作。但我选择了一个“行”,然后在不离开屏幕的情况下向下移动手指,然后离开屏幕,它不会改变该行的颜色。更好的解决方案?

我尝试过根据第一个答案使用选择器。它不起作用,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/directsubclass_row_selector"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/txt_text_view_directclass"
            android:layout_width="wrap_content"
            android:padding="10dp"
            android:layout_height="wrap_content" />
        <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray"/>
</LinearLayout>

选择代码:

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/abs__background_holo_light"/>
    <item android:drawable="@color/abs__background_holo_dark"/>
</selector>

2 个答案:

答案 0 :(得分:1)

这是我在我的一个项目中使用它的解决方案:

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                v.setBackgroundResource(R.drawable.pressed);
                lastY = event.getY();

                break;
            case MotionEvent.ACTION_UP:
                v.setBackgroundResource(R.drawable.normal);
                break;
            case MotionEvent.ACTION_MOVE:
                float abs = Math.abs(lastY - event.getY());

                if(abs > 5)
                    v.setBackgroundResource(R.drawable.normal);

                break;
        }

        return true;
    }
});

不是最干净的解决方案,但它的工作...... 您可以更改运动阈值(5)以满足您的应用程序要求。

答案 1 :(得分:0)

将选择器设置为背景,类似这样

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_focused="true" android:drawable="@color/red"/>
<item android:drawable="@color/white"/>

</selector>
相关问题