在onclicklistener中调用Switch

时间:2014-11-24 22:56:12

标签: java android

我正在创建一个基本游戏,我想在onClickListener中播放切换命令,如果。我无法让程序重复我想要的任务,即在单击按钮时更改颜色(黑白之间)。

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    final View vColor = (View) findViewById (R.id.vColor);
    Button bwhite = (Button) findViewById(R.id.bWhite);
    Button bblack = (Button) findViewById (R.id.bBlack);

    final Random colorBW = new Random();
    final int random = colorBW.nextInt(2);
    switch(random){
    case 0: 
        vColor.setBackgroundColor(Color.BLACK);         
        break;
    case 1:
        vColor.setBackgroundColor(Color.WHITE);
        break;
        };       

    bblack.setOnClickListener(new View.OnClickListener() {
        // 0 = BLACK
        // 1 = WHITE
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (random == 0){
                    THIS IS WHERE I WANT TO CALL MY SWITCH(RANDOM)  
            }else if (random == 1){
                Intent startGame = new Intent(MainActivity.this, com.random.shot.tapthecolor.MenuScreen.class);
                startActivity(startGame);
            }
        }
    });
    bwhite.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (random == 1){
                    doMySwitch(); 
            }else if (random == 0){
                Intent startGame = new Intent(MainActivity.this, com.random.shot.tapthecolor.MenuScreen.class);
                startActivity(startGame);
            }
        }
    });
    }

0 个答案:

没有答案