为什么按下时不改变颜色?

发布于 2024-12-06 17:19:05 字数 1196 浏览 1 评论 0原文

当您单击时,颜色应更改为另一种 但这不起作用! 我的代码:

#
   public class CreateActivity extends Activity {

TableLayout table;
Integer i;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
      table = (TableLayout)findViewById(R.id.myTable);

    Button left = (Button) findViewById(R.id.buttonLeft);
    Button right = (Button) findViewById(R.id.buttonRight);
    TextView color = (TextView) findViewById(R.id.text);

     i=0;

    right.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            // TODO Auto-generated method stub
        i++; //+1
        }
    });
       //COLOR
    switch(i){
    case 1: table.setBackgroundColor(Color.RED); break;
    case 2: table.setBackgroundColor(Color.rgb (255, 127, 0) ); break;
    case 3: table.setBackgroundColor(Color.YELLOW); break;
    case 4: table.setBackgroundColor(Color.GREEN) ; break;
    case 5: table.setBackgroundColor(Color.rgb (0,191,255) ); break;
    case 6: table.setBackgroundColor(Color.BLUE ); break;
    case 7: table.setBackgroundColor(Color.rgb (160,32,240) ); break;

    }
}

}

when you click the color should change to another
but it doesnt work!
My code:

#

   public class CreateActivity extends Activity {

TableLayout table;
Integer i;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
      table = (TableLayout)findViewById(R.id.myTable);

    Button left = (Button) findViewById(R.id.buttonLeft);
    Button right = (Button) findViewById(R.id.buttonRight);
    TextView color = (TextView) findViewById(R.id.text);

     i=0;

    right.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            // TODO Auto-generated method stub
        i++; //+1
        }
    });
       //COLOR
    switch(i){
    case 1: table.setBackgroundColor(Color.RED); break;
    case 2: table.setBackgroundColor(Color.rgb (255, 127, 0) ); break;
    case 3: table.setBackgroundColor(Color.YELLOW); break;
    case 4: table.setBackgroundColor(Color.GREEN) ; break;
    case 5: table.setBackgroundColor(Color.rgb (0,191,255) ); break;
    case 6: table.setBackgroundColor(Color.BLUE ); break;
    case 7: table.setBackgroundColor(Color.rgb (160,32,240) ); break;

    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-12-13 17:19:05

单击按钮时,您将递增 i - 但您不会再次运行 switch/case 语句。如果您查看调试器,您会发现变量值正在更改,但您没有指示显示的任何部分进行更改。

您应该将该通用逻辑放在一个单独的方法中,该方法可以从 onCreate 方法 OnClickListener 调用。例如:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    table = (TableLayout)findViewById(R.id.myTable);

    Button left = (Button) findViewById(R.id.buttonLeft);
    Button right = (Button) findViewById(R.id.buttonRight);
    TextView color = (TextView) findViewById(R.id.text);

    i=0;

    right.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            i++;
            // Cycle round
            if (i == 8) {
               i = 1;
            }
            applyBackgroundColor();
        }
    });

    applyBackgroundColor();
}

private void applyBackgroundColor() {
    switch(i) {
        // TODO: Consider what you want to do when i is 0...
        case 1: table.setBackgroundColor(Color.RED); break;
        case 2: table.setBackgroundColor(Color.rgb(255, 127, 0)); break;
        case 3: table.setBackgroundColor(Color.YELLOW); break;
        case 4: table.setBackgroundColor(Color.GREEN); break;
        case 5: table.setBackgroundColor(Color.rgb(0,191,255)); break;
        case 6: table.setBackgroundColor(Color.BLUE); break;
        case 7: table.setBackgroundColor(Color.rgb(160,32,240)); break;   
    }
}

关于这段代码,我还有很多其他的改变,但这至少应该能让你克服这个困难。

When the button is clicked, you're incrementing i - but you won't be running the switch/case statement again. If you look in the debugger you'll find that the variable value is changing, but you haven't instructed any part of the display to change.

You should put that common logic in a separate method which is called both from the onCreate method and the OnClickListener. For example:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    table = (TableLayout)findViewById(R.id.myTable);

    Button left = (Button) findViewById(R.id.buttonLeft);
    Button right = (Button) findViewById(R.id.buttonRight);
    TextView color = (TextView) findViewById(R.id.text);

    i=0;

    right.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            i++;
            // Cycle round
            if (i == 8) {
               i = 1;
            }
            applyBackgroundColor();
        }
    });

    applyBackgroundColor();
}

private void applyBackgroundColor() {
    switch(i) {
        // TODO: Consider what you want to do when i is 0...
        case 1: table.setBackgroundColor(Color.RED); break;
        case 2: table.setBackgroundColor(Color.rgb(255, 127, 0)); break;
        case 3: table.setBackgroundColor(Color.YELLOW); break;
        case 4: table.setBackgroundColor(Color.GREEN); break;
        case 5: table.setBackgroundColor(Color.rgb(0,191,255)); break;
        case 6: table.setBackgroundColor(Color.BLUE); break;
        case 7: table.setBackgroundColor(Color.rgb(160,32,240)); break;   
    }
}

There are various other things I'd change about this code, but that should at least get you over this hump.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文