Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓:按下按钮时不会改变颜色_Java_Android_Button - Fatal编程技术网

Java 安卓:按下按钮时不会改变颜色

Java 安卓:按下按钮时不会改变颜色,java,android,button,Java,Android,Button,我在我的drawable文件夹中创建了一个custom_button.xml,用于按钮按下时的显示和默认设置。情况如下: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape> <gradient android:startColor

我在我的
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>
<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"/>

这是我使用的自定义_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()
无法正确使用选择器。您必须在“下一步”和“上一步”中手动更改背景。

最好看看这是否会产生影响。您也可以调用按钮的setBackground函数。