Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.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
Android 按钮-SetBackground()单击后消失_Android_Android Layout - Fatal编程技术网

Android 按钮-SetBackground()单击后消失

Android 按钮-SetBackground()单击后消失,android,android-layout,Android,Android Layout,在我的应用程序中,我只是简单地尝试将一个按钮的背景画成另一个形状非常相似的黄色背景,这意味着按钮被按下了。 我不认为我可以使用选择器来实现这一点,因为实际上我有多个按钮,随着时间的推移,必须处理多次单击,所以对我来说,最好的选择是在代码中实现这一点 但是,当单击按钮时,背景会按预期变为黄色,但它会立即返回白色默认背景可绘制,即使我希望它保持黄色背景,直到我将其设置回默认背景 有什么问题吗?可能是选择器我没有设置它 Java代码 myVisibility = false;

在我的应用程序中,我只是简单地尝试将一个按钮的背景画成另一个形状非常相似的黄色背景,这意味着按钮被按下了。 我不认为我可以使用选择器来实现这一点,因为实际上我有多个按钮,随着时间的推移,必须处理多次单击,所以对我来说,最好的选择是在代码中实现这一点

但是,当单击按钮时,背景会按预期变为黄色,但它会立即返回白色默认背景可绘制,即使我希望它保持黄色背景,直到我将其设置回默认背景

有什么问题吗?可能是选择器我没有设置它

Java代码

        myVisibility = false;
        myHighlighting = false;
        myContext = context;
        //noinspection Convert2Lambda
        myOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!myVisibility && !myHighlighting) {
                    discoverLetterButton();
                } else if (myVisibility && !myHighlighting) {
                    /*other code, not important
                    word += myCharacter;
                    WordTextView.setText(word);*/

                    /*here is the problem: the following line is executed but then the button immediately returns to the default drawable. */

                    v.setBackgroundDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.letter_button_front_highlighted, null));
                    //myButton.setClickable(false);
                }
            }
        };
        myButton.setOnClickListener(myOnClickListener);
        myButton.setClickable(true);
letter_button_front.xml默认背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
    android:radius="7dp"
    />
<solid
    android:color="#ffffff" //White
    />
<stroke
    android:width="3dp"
    android:color="#000000"
    />
</shape>
letter_button_front_highlighted.xml我正在设置的形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
    android:radius="7dp"
    />
<solid
    android:color="#ffffb32f" //Orange-yellow
    />
<stroke
    android:width="3dp"
    android:color="#000000"
    />
</shape>

提前感谢您的帮助

由于其更改,因此正在使用onClickListener并设置新的BG。因此,您显示的代码不是问题所在。它要么在其他地方被重置(如通过另一个setBackgroundDrawable调用),要么视图在其他地方被重新创建为默认值。我知道你的意思,但是我已经一次又一次地检查了java类,但是我仍然没有找到颜色变化的原因…这就是为什么我认为问题出在选择器上。我终于开始工作了…问题出在动画监听器上,因为它的ONED事件被无休止地调用。不要问我为什么,这可能是android代码中的一个错误。