2012-04-20 87 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) ... 

    } 
}