繁体   English   中英

Android:按下时按钮不会更改颜色

[英]Android: Button does not change color when pressed

我已经在我的drawable文件夹中创建了一个custom_button.xml ,用于何时按下按钮以及默认情况。 它看起来如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" >
    <shape>
        <gradient
            android:startColor="@color/red"
            android:endColor="@color/light_red"
            android:angle="270" />
        <corners
            android:radius="5dp" />
    </shape>
</item>

<item>
    <shape>
        <gradient
            android:startColor="@color/light_purple"
            android:endColor="@color/purple"
            android:angle="270" />
        <corners
            android:radius="5dp" />
    </shape>
</item>

未按下时,我的按钮看起来不错,它显示了默认的紫色按钮。 但是当我按下它时,它并不会变成红色按钮,就像state_pressed时应该是红色按钮一样。

在我的activity_main.xml ,按钮定义如下:

<Button
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@drawable/custom_button"
    android:layout_margin="5dp"/>

我正在使用嵌套的LinearLayout如果有什么区别。

请让我知道我是否错过了什么!

编辑:我发现了问题的根源,但不确定如何解决。 当我从createListeners删除以下这些侦听器时,按钮会根据需要更改颜色。

 b1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                int beat = getBeat(1);
                m1 = MediaPlayer.create(MainActivity.this, beat);
                m1.start();
                m1.setLooping(true);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                m1.release();
            }
            return false;
        }
    });

它的基本作用是只要按下按钮,它就会播放一首歌曲。 这如何影响按钮的颜色?

我尝试了您的选择器,它正在我的身边工作。 我注意到您按钮的宽度为0dp,当我在一侧使用该XML时,它不起作用。

尝试将其更改为此:

<Button
    android:id="@+id/button1"
    android:layout_width="warap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="test"
    android:background="@drawable/custom_button"
    android:layout_margin="5dp"/>

这是我使用的custom_buttom.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape>
            <gradient
                android:startColor="#FF0000"
                android:endColor="#FF00FF"
                android:angle="270" />
        </shape>

    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#1E669B"
                android:endColor="#1E669B"
                android:angle="270" />

        </shape>
    </item>
</selector>

好吧,所以我设法使其工作了,但这可能是一种回旋的方式。 我基本上改变了setOnTouchListener所以看起来像这样:

b1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                int beat = getBeat(1);
                m1 = MediaPlayer.create(MainActivity.this, beat);
                m1.start();
                m1.setLooping(true);
                b1.setPressed(true);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                b1.setPressed(false);
                m1.release();
            }
            return false;
        }
    });

只需手动更改按钮的按下状态。

似乎onTouchListener()无法与选择器一起正常使用。 您必须在ACTION_DOWN和ACTION_UP中手动更改背景。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM