2012-04-20 89 views
0

我正在嘗試更改按鈕上文本的顏色。這是我的代碼Android onClick不會更改按鈕的文本顏色

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

public class ColorPatternGameActivity extends Activity implements OnClickListener { 
protected Button button1; 
protected Button button2; 
protected Button button3; 
protected Button button4; 
protected TextView test; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Button button1 = (Button)findViewById(R.id.b1); 
    Button button2 = (Button)findViewById(R.id.b2); 
    Button button3 = (Button)findViewById(R.id.b3); 
    Button button4 = (Button)findViewById(R.id.b4); 
    TextView test = (TextView)findViewById(R.id.test); 

    button1.setOnClickListener(this); 
    button2.setOnClickListener(this); 
    button3.setOnClickListener(this); 
    button4.setOnClickListener(this); 

    test.setText("testing"); 

} 

@Override 
public void onClick(View v) { 
    if (v == button1) { 
     test.setText("Red"); 
     button1.setTextColor(Color.RED); 
     } 
    else if (v == button2) { 
     button2.setTextColor(Color.GREEN); 
     test.setText("Green"); 
    } 
    else if (v == button3) { 
     button3.setTextColor(Color.BLUE); 
     test.setText("Blue"); 
    } 
    else if (v == button4) { 
     button4.setTextColor(Color.YELLOW); 
     test.setText("Yellow"); 
    } 
} 
} 

單擊按鈕不會更改文本或顏色。我之前已經實現了onClick,它工作的很好,這讓我更加困惑,爲什麼它不工作。

如果它幫助我的XML的樣本,我宣佈一個按鈕:

<Button 
android:id="@+id/b1" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:layout_weight="1" 
android:text="1"/> 

回答

4

你分配局部變量findViewById結果。由於您正在使用成員變量在onClick方法中測試引用等式,所以這將始終是錯誤的。

變化(和其他人太):

Button button1 = (Button)findViewById(R.id.b1); 

要:

button1 = (Button)findViewById(R.id.b1); 

雖然,它很可能是清潔劑,如果你只是不得不爲每個按鈕的點擊不同的方法。

+0

Facepalm,其總是簡單的東西從來沒有大的東西。謝謝wsanville。 – Phil 2012-04-20 19:21:45

0

您初始化類變量

protected Button button1; 
protected Button button2; 
protected Button button3; 
protected Button button4; 

然後onCreate方法,則需要重新初始化你的按鈕

Button button1 = (Button)findViewById(R.id.b1); 
Button button2 = (Button)findViewById(R.id.b2); 
Button button3 = (Button)findViewById(R.id.b3); 
Button button4 = (Button)findViewById(R.id.b4); 

,這使得本地onCreate方法的按鈕,這不應該做的,在你的如果類變量沒有被分配任何事情做,所以你的代碼不工作...

我懷疑這不會發生,如果y OU已成立onClickListener的裏面的onCreate本身,因爲這將是您的本地方法的文本顏色會被改變......

因此,而不是使用的Button button1 = (Button)findViewById(R.id.b1);中的onCreate使用button1 = (Button)findViewById(R.id.b1);

如果您使用的是Eclipse IDE,那麼Button1的將是藍色表明它是一類變...

0

你可以看到這個例子

<Button android:id="@+id/testing" 
     android:text="Testing" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#000000"> 

下面按鈕的文字顏色可以是黑色現在我將其更改爲白顏色的按鈕點擊

@Override 
    public void onClick(View v) { 
     if (v == testing) { 
     testing.setTextColor(Color.WHITE); 
     } 
    } 
1

如果要創建按鈕親語法與否,這種解決方案會奏效。

只是使用鑄造物體((Button)v).setTextColor(Color.GRAY);如下所示:

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Toast toast; 
    Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId()); 
    switch (v.getId()) { 
    case MY_BUTTON: 
     toast = Toast.makeText(this, "Clicked on " + v.getTag().toString(), Toast.LENGTH_LONG); 
     toast.setGravity(Gravity.TOP, 25, 400); 
     toast.show(); 

     ((Button) v).setTextColor(Color.GRAY); 

     v.setBackgroundColor(Color.WHITE); 


     break; 
     // More buttons go here (if any) ... 

    } 
}