2012-09-27 55 views
6

enter image description heresetBackgroundColor android系統

在這個簡單的遊戲,我想改變我按下按鈕的背景顏色。但我得到以下結果,按鍵外觀變得不好(形狀變得不同):

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

發表你的代碼!編輯過的 – Shrikant

+0

,我添加了完整的代碼。 –

+0

感謝問 - 這正是我的問題! –

回答

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

PorterDuff類未定義。我應該輸入什麼? –

+2

import android.graphics.PorterDuff; –

+1

不錯,現在它工作。 –

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"/> 
6

與梯度編輯

<?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_selector.xml任何名稱

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

感謝您的回答,但我真的得到了幾乎以前的結果。 –

+0

如果這是正確的,那麼你必須接受一個最接近你的解決方案的答案 – Pratik

+0

我試着一個接一個地回答所有的答案。到目前爲止,它們都將按鈕的形狀改變成方形。 –

1

作爲@pratik說,拯救這個button.xml在繪製文件夾

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