以编程方式更改按钮背景

时间:2017-03-20 14:55:09

标签: android background android-button

我正在使用Android Studio创建一个Tic-Tac-Toe应用程序,这是布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.company.alex.tic_tac_toe.MainActivity"
android:layout_margin="16dp"
android:layout_marginBottom="16dp">

<Button
    android:id="@+id/B2"
    android:background="#33b5e5"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:textSize="@dimen/activity_vertical_margin"
    tools:textStyle="normal"
    tools:textSize="36sp" />

<Button
    android:layout_height="80dp"
    android:layout_above="@+id/B2"
    android:layout_centerHorizontal="true"
    android:id="@+id/A2"
    android:layout_marginBottom="16dp"
    android:background="#33b5e5"
    android:layout_width="80dp" />

<Button
    android:layout_alignBottom="@+id/B2"
    android:layout_toEndOf="@+id/B2"
    android:id="@+id/B3"
    android:background="#33b5e5"
    android:layout_marginLeft="16dp"
    android:layout_width="80dp"
    android:layout_height="80dp"
    tools:textSize="@android:dimen/thumbnail_width" />

<Button
    android:id="@+id/B1"
    android:layout_below="@+id/A2"
    android:layout_toStartOf="@+id/B2"
    android:layout_marginRight="16dp"
    android:background="#33b5e5"
    android:layout_width="80dp"
    android:layout_height="80dp" />

<Button
    android:layout_alignTop="@+id/A2"
    android:layout_alignEnd="@+id/B1"
    android:id="@+id/A1"
    android:background="#33b5e5"
    android:layout_width="80dp"
    android:layout_height="80dp" />

<Button
    android:id="@+id/C2"
    android:background="#33b5e5"
    android:layout_below="@+id/B2"
    android:layout_toEndOf="@+id/B1"
    android:layout_marginTop="16dp"
    android:layout_width="80dp"
    android:layout_height="80dp" />

<Button
    android:layout_alignTop="@+id/C2"
    android:layout_toStartOf="@+id/C2"
    android:id="@+id/C1"
    android:background="#33b5e5"
    android:layout_marginRight="16dp"
    android:layout_width="80dp"
    android:layout_height="80dp" />

<Button
    android:layout_alignTop="@+id/C2"
    android:layout_toEndOf="@+id/C2"
    android:id="@+id/C3"
    android:layout_marginLeft="16dp"
    android:background="#33b5e5"
    android:layout_width="80dp"
    android:layout_height="80dp" />

<Button
    android:text="NUOVA PARTITA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:id="@+id/bNewGame"
    android:layout_alignParentEnd="true"
    android:background="#f17a0a"
    tools:textSize="24sp" />

<Button
    android:layout_height="80dp"
    android:id="@+id/A3"
    android:background="#33b5e5"
    android:layout_width="80dp"
    android:layout_alignTop="@+id/A2"
    android:layout_alignStart="@+id/B3" />

<TextView
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="18dp"
    android:id="@+id/punteggio"
    android:layout_width="80dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true"
    android:textAppearance="@style/TextAppearance.AppCompat" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/grigio"
    android:id="@+id/imageView5"
    android:layout_alignBottom="@+id/C2"
    android:layout_alignEnd="@+id/B3"
    android:layout_alignTop="@+id/A2"
    android:layout_alignStart="@+id/B1"
    android:background="@drawable/grigio" />

</RelativeLayout>

我希望在玩家获胜时更改三个按钮的背景颜色。 我在这部分代码中检查获胜者

`{
    boolean there_is_a_winner = false;
    //prima si controllano le linee orizzontali
    if (a1.getText() == a2.getText() && a2.getText() == a3.getText() && !a1.isClickable())
        there_is_a_winner = true;
    else if (b1.getText() == b2.getText() && b2.getText() == b3.getText() && !b1.isClickable())
        there_is_a_winner = true;
    else if (c1.getText() == c2.getText() && c2.getText() == c3.getText() && !c1.isClickable())
        there_is_a_winner = true;


        //adesso si va a controllare le linee verticali
    if (a1.getText() == b1.getText() && b1.getText() == c1.getText() && !a1.isClickable())
        there_is_a_winner = true;
    else if (a2.getText() == b2.getText() && b2.getText() == c2.getText() && !b2.isClickable())
        there_is_a_winner = true;
    else if (a3.getText() == b3.getText() && b3.getText() == c3.getText() && !c3.isClickable())
        there_is_a_winner = true;

        //adesso si controllano le diagonali
    if (a1.getText() == b2.getText() && b2.getText() == c3.getText() && !a1.isClickable())
        there_is_a_winner = true;
    else if (a3.getText() == b2.getText() && b2.getText() == c1.getText() && !b2.isClickable())
        there_is_a_winner = true;`

如果我把“Button11.setBackgroundColor(Color.RED);”如果程序失败,在第一个下面 我怎么能这样做?

0 个答案:

没有答案