可绘制选择器+ TextColor

时间:2016-08-08 04:14:05

标签: android selector android-drawable

enter image description here

在上图中,2个按钮具有相同的可绘制选择器。第一个没有按下时,另一个按下时。 如何更改TextColor取决于按下的状态 这是布局

   <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="40dp">
       <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="40dp">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/buttonback"
                android:clickable="true"
                android:id="@+id/l1"
                android:onClick="dialog">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Individual"
                    android:id="@+id/t1"
                    android:layout_gravity="center"
                    android:textSize="18dp"
                    android:textStyle="bold"
                    android:textAlignment="center"
                    android:onClick="dialog"
                    android:textColor="#000000" />

               <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="SR 19.99/month after trial ends"
                android:id="@+id/t2"
                android:layout_gravity="center"
                android:textSize="16dp"
                android:textAlignment="center"
                android:onClick="dialog"
                android:textColor="#898989" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/buttonback"
            android:layout_marginTop="25dp"
            android:clickable="true"
            android:id="@+id/l2"
            android:onClick="dialog">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Family (Up to 6 members)"
                android:id="@+id/t3"
                android:layout_gravity="center"
                android:textSize="18dp"
                android:textStyle="bold"
                android:textAlignment="center"
                android:onClick="dialog"
                android:textColor="#000000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="SR 29.99/month after trial ends"
                android:id="@+id/t4"
                android:layout_gravity="center"
                android:textSize="16dp"
                android:textAlignment="center"
                android:onClick="dialog"
                android:textColor="#898989" />
        </LinearLayout>

    </LinearLayout>

这是Selector drawable

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <shape android:shape="rectangle"
        xmlns:android="http://schemas.android.com/apk/res/android">
        <stroke android:color="#ff395d" android:width="1dp"></stroke>
        <solid android:color="#ff395d"></solid>
        <corners android:radius="60dp"></corners>
        <padding
            android:top="15dp"
            android:right="15dp"
            android:left="15dp"
            android:bottom="15dp"
            ></padding>
    </shape>
</item>
<item android:state_pressed="false">
    <shape android:shape="rectangle">
        <stroke android:color="#ff395d" android:width="1dp"></stroke>
        <solid android:color="#ffffff"></solid>
        <corners android:radius="60dp"></corners>
        <padding
            android:top="15dp"
            android:right="15dp"
            android:left="15dp"
            android:bottom="15dp"
            ></padding>
    </shape>
  </item>
</selector>

我希望我的代码在图片中工作,但我不知道如何更改TextColor

2 个答案:

答案 0 :(得分:0)

您可以使用以下可绘制文件更改文字颜色。

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

请设置这样的drawable。

            <TextView
                android:id="@+id/txtView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello"
                android:textColor="@drawable/selector_text" />

并且您必须使用setSelected的{​​{1}}属性来更改Java代码中的textView颜色。

答案 1 :(得分:0)

一种解决方案是扩展处理按下状态的父布局并给出回调。如果您在布局中有ImageView并希望根据按下状态更改色调颜色,则此功能非常有用。

接口:

public interface PressedListener {
    void onDown();

    void onUp();
}

布局类:

public class MyRelativeLayout extends RelativeLayout {

    private PressedListener mPressedListener;
    private Rect mViewRect;

    public MyRelativeLayout(Context context) {
        this(context, null);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setPressedListener(PressedListener listener) {
        mPressedListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mPressedListener != null) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mPressedListener.onDown();
                    mViewRect = new Rect();
                    getHitRect(mViewRect);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_OUTSIDE:
                    mPressedListener.onUp();
                    mViewRect = null;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (mViewRect != null &&
                            !mViewRect.contains(Math.round(getX() + event.getX()),
                                    Math.round(getY() + event.getY()))) {
                        mPressedListener.onUp();
                        mViewRect = null;
                    }

                    break;
            }
        }

        return super.onTouchEvent(event);
    }
}