1

我创建了一个应用程序,假设每次单击时都会切换按钮背景颜色。这是代码:

package com.example.flash.light;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button screen = (Button) findViewById(R.id.screen);
        Drawable background = getResources().getDrawable(R.drawable.black);
        screen.setBackgroundDrawable(background);

        screen.setOnClickListener(new Button.OnClickListener(){ 
            public void onClick(View v) { 
                Button screen = (Button) findViewById (R.id.screen);
                Drawable background = screen.getResources().getDrawable(screen.getId());
                if(background == getResources().getDrawable(R.drawable.black)){
                    Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
                    screen.setBackgroundDrawable(background);
                }
                if(background == getResources().getDrawable(R.drawable.white)){
                    Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
                    screen.setBackgroundDrawable(background);
                }
            } 
        }); 
    }

点击按钮没有反应。谢谢

4

2 回答 2

2

我相信这里的问题是您返回的资源

getResources().getDrawable(int id)

每次调用都不一样。Android 只是构建可绘制类的新实例,而不是返回旧实例。(至少我相信在这种情况下没有缓存)

用 == 运算符比较三个不同的实例永远不会返回 true。

第二件事是代码中的一个明显错误:

            Button screen = (Button) findViewById (R.id.screen);
            Drawable background = screen.getResources().getDrawable(screen.getId());
            if(background == getResources().getDrawable(R.drawable.black)){
                Drawable newBackgroun = getResources().getDrawable(R.drawable.white);
                screen.setBackgroundDrawable(**newBackground**);
            }
            if(background == getResources().getDrawable(R.drawable.white)){
                Drawable newBackgroun = getResources().getDrawable(R.drawable.black);
                screen.setBackgroundDrawable(**newBackground**);
            }

而不是背景,你应该有 newBackground 那里。

于 2012-11-02T16:24:35.047 回答
1

尝试这个 :

public class MainActivity extends Activity {

    private boolean isBlack = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        // your code

        final Button screen = (Button) findViewById (R.id.screen);
        isBlack = true;
        screen.setBackgroundColor(Color.BLACK);
        screen.setOnClickListener(new Button.OnClickListener(){ 
            public void onClick(View v) { 
                if (isBlack){ 
                    screen.setBackgroundColor(Color.WHITE);
                    isBlack = false;
                }
                else{
                    screen.setBackgroundColor(Color.BLACK);
                    isBlack = true;
                }
            }

        });
}
}
于 2012-11-02T16:54:28.017 回答