java - 为什么这个开关盒不点火?

标签 java android switch-statement

我有一整行与 oncheckboxclickeds 相关的 switch case,我用它来总计一个数字。

该行中的每个案例都工作得很好,除了最后一个,它似乎根本没有做任何事情,我不明白为什么。

这一切都很好,直到最后:

package com.example.blazingboltcalculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;


public class MainActivity extends Activity {



int attack=11;
int damage=3;
int flankAttack;
int pbs;
int justice;
int destruction;
int bane;
int DF;
int GMW;
int heroism;
int deadly;
int attackTotal;
int damageTotal;

String flames="";
String baneDice="";
String keen="";
String diceTotal;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}




public void onCheckboxClicked(View v) {
    // Is the view now checked?
    boolean checked = ((CheckBox) v).isChecked();


    // Check which checkbox was clicked
    switch(v.getId()) {

    case R.id.checkBox1:
        if (checked)
                {
                flankAttack=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }

        else
                {
                flankAttack=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
        break;

    case R.id.checkBox2:
        if (checked)
                {
                pbs=1; 
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
        else
                {
                pbs=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
        break;

    case R.id.checkBox3:
            if (checked)
            {
                justice=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
               }
            else
            {
                justice=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox4:
            if (checked)
            {
                destruction=4;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            else
            {
                destruction=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox5:
            if (checked)
            {
                bane=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;
                baneDice="+2d6";
                diceTotal="1d8"+baneDice+flames+keen;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                textView3.setText(diceTotal);
                }
            else
            {
                bane=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;
                baneDice="";
                diceTotal="1d8"+baneDice+flames+keen;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                textView3.setText(diceTotal);
                }
            break;

        case R.id.checkBox6:
            if (checked)
            {
                DF=3;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            else
            {
                DF=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox7:
            if (checked)
            {
                flames="+1d6 fire";
                diceTotal="1d8"+baneDice+flames+keen;
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView3.setText(diceTotal);
            }
            else
            {
                flames="";
                diceTotal="1d8"+baneDice+flames+keen;
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView3.setText(diceTotal);
            }
            break;

        case R.id.checkBox8:
            if (checked)
            {
                GMW=1;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            else
            {
                GMW=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox9:
            if (checked)
            {
                heroism=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
               }
            else
            {
                heroism=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
                }
            break;

        case R.id.checkBox10:
            if (checked)
            {
                keen="+19-20 crit";
                diceTotal="1d8"+baneDice+flames+keen;
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView3.setText(diceTotal);
            }
            else
            {
                keen="";
                diceTotal="1d8"+baneDice+flames+keen;
                TextView textView3 = (TextView) findViewById(R.id.textView6);
                textView3.setText(diceTotal);
            }
            break;

        case R.id.checkBox11:
            if (checked)
            {
                deadly=2;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
            }
            else
            {
                deadly=0;
                attackTotal=attack+flankAttack+pbs+justice+bane+DF+GMW+heroism+deadly;
                damageTotal=damage+pbs+destruction+bane+DF+GMW+deadly;

                TextView textView = (TextView) findViewById(R.id.textView2);
                TextView textView2 = (TextView) findViewById(R.id.textView4);
                textView.setText(Integer.toString(attackTotal));
                textView2.setText(Integer.toString(damageTotal));
            }
    }
}

}

我对编程有点陌生,有人知道什么可能会搞砸工作吗?除了最后一个案例之外,整个困惑工作完美。

最佳答案

嗯,我们需要更多信息来帮助您。但是,我假设您正在尝试使用 OnCheckedChangeListener这是由您的主要 Activity 实现的?如果您在引用 ID 时遇到问题,您始终可以在特定元素上实现单独的监听器,例如:

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
   {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if ( isChecked )
            {

                prevChecked = true;
        Log.d("CHECKBOX","Previously Checked = "+prevChecked);
            }else{

                prevChecked = false;
        Log.d("CHECKBOX","Previously Checked = "+prevChecked);

            }

        }
    });

其中 prevChecked 是全局变量。我还添加了几个 Log,以便您可以将信息输出到 LogCat。希望对您的调试有所帮助。

***** 编辑以适合您添加的评论*************

您是否尝试过将 switch 语句与 if 语句交换。例如

if(v.getId() == R.id.checkbox1){

}else if(v.getId() == R.id.checkbox2){

}

另外,onCheckboxClicked(View v)是什么接口(interface),这是自定义元素吗?您如何在 R.layout.activity_main.xml 中定义复选框?如果您有一个复选框接口(interface),您需要实现一个监听器,就像我上面的示例一样。我看不到您在任何地方设置了监听器,也没有在您的 Activity 中实现它。例如:

public class MainActivity extends Activity implements onCheckChangedListener {

但据我所知,除非它是自定义元素,否则调用 public void onCheckboxClicked(View v) 甚至 get 都没有任何意义。这个example可能有帮助。

关于java - 为什么这个开关盒不点火?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11713916/

相关文章:

java - 在 IntelliJ GUI Builder 中打开现有源代码

android - 带边框的网格布局

java - 使用 DiskLruCache 时出现正则表达式错误 (Jake Wharton)

reporting-services - 在SSRS 2008中的开关表达式中使用AND

java - 如何对使用 session 窗口的 kafka 流应用程序进行单元测试

java - 如何将类传递给方法并调用该类的静态方法

带有自定义比较器的 java TreeSet 不起作用

java - 使用 ViewPageIndicator 的 SherlockFragment 内的 Tabhost

java - 将 if 字符串语句转换为 switch 字符串语句

javascript - Switch 在 JavaScript 中给出错误的值