在安卓中设置背景颜色

7
在这个简单的游戏中,我想要改变我按下的按钮的背景颜色。但是,我得到了以下结果,按钮的外观变得不好看(形状变得不同):
pressedButton.setBackgroundColor(Color.RED);

有更好的方法吗?谢谢。

[编辑:我的完整代码]

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;
    }
}

帖子已编辑,我添加了完整的代码。 - Hamzeh Soboh
谢谢你的提问 - 那正是我想问的问题! - Martin Frank
4个回答

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

1
PorterDuff类未定义。我需要导入什么吗? - Hamzeh Soboh
2
import android.graphics.PorterDuff; - Tamir Scherzer

6
在drawable文件夹中创建一个选择器文件,可将其命名为button_selector.xml。 使用Gradient编辑完毕
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <gradient android:startColor="#ad1c1c" android:endColor="#cc3737" android:angle="90"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#7d0000"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <gradient android:startColor="#cfcfcf" android:endColor="#ebebeb" android:angle="90"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#8f8f8f"/>
        </shape>
    </item>
</selector>

然后设置按钮背景

<Button
    android:background="@drawable/button_selector"
/>

谢谢您的回答,但实际上我得到的几乎是之前的结果。 - Hamzeh Soboh
如果正确的话,您必须接受与您的解决方案最接近的答案之一。 - Pratik
我正在依次尝试所有的答案。到目前为止,它们都将按钮的形状改变成了正方形。 - Hamzeh Soboh

1

正如@pratik所说,将此button.xml保存在drawable文件夹中

button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="#f00"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#222"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="#f1f1f1"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#222"/>
        </shape>
    </item>
</selector>

将此按钮应用为背景

<Button
    android:background="@drawable/button"/>

在你的类文件中,像这样做

public void onClick(View v) {

  pressedButton.setPressed(true);
}

这样红色就会稳定下来


1
嗯,复制答案并保存它,但你需要一些技巧来避免被原始用户捕获,可以通过将复制的答案更改为其他值来实现。 - Pratik

0

试试这个:

在ImageButton的xml中,你需要创建像这样的内容

使用按钮图像创建xml文件,就像这样

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item 
android:state_pressed="true" 
android:drawable="@drawable/backbutton" />
<item 
android:drawable="@drawable/closebutton" />
</selector> 

将那个 XML 文件作为 ImageButton 的背景添加。
<ImageButton                 
android:layout_height="50px" 
android:layout_width="50px" 
android:id="@+id/settings" 
android:background="@drawable/settings_button" //setting_button in
                                                     the xml file            
android:text="Settings"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接