【问题标题】:Android: Button does not change color when pressedAndroid:按下时按钮不会改变颜色
【发布时间】:2015-05-14 21:31:52
【问题描述】:

我在我的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;
        }
    });

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

【问题讨论】:

标签: java android button


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我试过你的选择器,它在我这边工作。 我注意到你的按钮的宽度是 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>
    

    【讨论】:

    • 也可以只调用Button的setBackground函数。
    【解决方案3】:

    好的,所以我已经设法让它工作了,但这可能是一种迂回的方式。我基本上改变了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;
            }
        });
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      相关资源
      最近更新 更多