setBackgroundColor在安卓 [英] setBackgroundColor in android

查看:131
本文介绍了setBackgroundColor在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个简单的游戏我想改变我preSS按钮的背景颜色。但我得到以下结果,按键外观变得不好(形状变得不同):

  pressedButton.setBackgroundColor(Color.RED);
 

有没有一种更好的方式来做到这一点?谢谢你。

 包com.example.xo_game;

进口android.os.Bundle;
进口android.app.Activity;
进口android.graphics.Color;
进口android.view.Menu;
进口android.view.View;
进口android.widget.Button;

公共类MainActivity延伸活动{

    按钮[] btns;
    的char [] []游戏状态=新的char [3] [3];
    烧焦转=X;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        按钮[] btns =新按钮[9];

        btns [0] =(按钮)findViewById(R.id.btn1);
        btns [1] =(按钮)findViewById(R.id.btn2);
        btns [2] =(按钮)findViewById(R.id.btn3);

        btns [3] =(按钮)findViewById(R.id.btn4);
        btns [4] =(按钮)findViewById(R.id.btn5);
        btns [5] =(按钮)findViewById(R.id.btn6);

        btns [6] =(按钮)findViewById(R.id.btn7);
        btns [7] =(按钮)findViewById(R.id.btn8);
        btns [8] =(按钮)findViewById(R.id.btn9);

        的for(int i = 0; I< 9;我++){
            btns [I] .setTag(ⅰ);
            btns [I] .setOnClickListener(clickListener);
            游戏状态[I / 3] [我%3] ='E';
        }
    }

    View.OnClickListener clickListener =新View.OnClickListener(){

        公共无效的onClick(视图v){
            按钮pressedButton =(按钮)V;

            INT的indexOf pressedButton =的Integer.parseInt(pressedButton.getTag()
                    的ToString());

            INT行=的indexOf pressedButton / 3;
            INT COL =的indexOf pressedButton%3;

            如果(游戏状态[行] [山口]!='E')
                返回;

            游戏状态[行] [COL] =转;

            字符串turnAsString =将String.valueOf(转);

            pressedButton.setText(turnAsString);

            如果(置于=='X'){
                pressedButton.setBackgroundColor(Color.RED);
                转='O';
            } 其他 {
                pressedButton.setBackgroundColor(Color.GREEN);
                转=X;
            }
        }
    };

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
}
 

解决方案

  pressedButton.getBackground()setColorFilter(Color.RED,PorterDuff.Mode.MULTIPLY)。
 

In this simple game I want to change the background color of the button that I press. But I get the following result, the buttons appearance becomes not good (the shape becomes different):

pressedButton.setBackgroundColor(Color.RED);

Is there a nicer way to do that? Thanks.

[Edit: my full code]

package com.example.xo_game;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    Button[] btns;
    char[][] gameState = new char[3][3];
    char turn = 'X';

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

        Button[] btns = new Button[9];

        btns[0] = (Button) findViewById(R.id.btn1);
        btns[1] = (Button) findViewById(R.id.btn2);
        btns[2] = (Button) findViewById(R.id.btn3);

        btns[3] = (Button) findViewById(R.id.btn4);
        btns[4] = (Button) findViewById(R.id.btn5);
        btns[5] = (Button) findViewById(R.id.btn6);

        btns[6] = (Button) findViewById(R.id.btn7);
        btns[7] = (Button) findViewById(R.id.btn8);
        btns[8] = (Button) findViewById(R.id.btn9);

        for (int i = 0; i < 9; i++) {
            btns[i].setTag(i);
            btns[i].setOnClickListener(clickListener);
            gameState[i / 3][i % 3] = 'E';
        }
    }

    View.OnClickListener clickListener = new View.OnClickListener() {

        public void onClick(View v) {
            Button pressedButton = (Button) v;

            int indexOfPressedButton = Integer.parseInt(pressedButton.getTag()
                    .toString());

            int row = indexOfPressedButton / 3;
            int col = indexOfPressedButton % 3;

            if (gameState[row][col] != 'E')
                return;

            gameState[row][col] = turn;

            String turnAsString = String.valueOf(turn);

            pressedButton.setText(turnAsString);

            if (turn == 'X') {
                pressedButton.setBackgroundColor(Color.RED);
                turn = 'O';
            } else {
                pressedButton.setBackgroundColor(Color.GREEN);
                turn = 'X';
            }
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

解决方案

pressedButton.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);

这篇关于setBackgroundColor在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆