【问题标题】:Android onClick does not change the text color of buttonsAndroid onClick 不会改变按钮的文字颜色
【发布时间】:2012-05-02 09:02:33
【问题描述】:

我正在尝试更改按钮上文本的颜色。这是我的代码

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

【问题讨论】:

    标签: android xml button onclick onclicklistener


    【解决方案1】:

    您正在用findViewById 的结果分配局部变量。由于您正在使用成员变量在 onClick 方法中测试引用相等性,因此这将始终为 false。

    改变(以及其他):

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

    收件人:

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

    不过,如果您为每个按钮设置不同的点击方法,可能会更简洁。

    【讨论】:

    • Facepalm,它总是简单的东西,而不是大的东西。谢谢你wsanville。
    【解决方案2】:

    无论您是否以编程方式创建按钮,此解决方案都可以使用。

    只需使用投射对象 ((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) ...
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      你像

      这样初始化类变量
      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 方法的本地按钮,不应该这样做,在你的情况下,类变量没有被分配任何事情要做,所以你的代码不起作用......

      我怀疑如果您在 onCreate 本身中设置了 onClickListener 就不会发生这种情况,因为它对于您的方法来说是本地的,文本颜色会被更改...

      所以不要使用Button button1 = (Button)findViewById(R.id.b1);in onCreate 使用button1 = (Button)findViewById(R.id.b1);

      如果您使用的是 Eclipse IDE,那么 button1 将是蓝色的,表示它是一个类变量...

      【讨论】:

        【解决方案4】:

        你可以看看这个例子

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

        【讨论】:

          猜你喜欢
          • 2013-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-02
          • 1970-01-01
          • 1970-01-01
          • 2015-12-01
          • 2015-01-12
          相关资源
          最近更新 更多