为什么这个开关盒没有开火?

时间:2012-07-29 23:45:24

标签: java android switch-statement

我有一整行的开关案例与oncheckboxclickeds相关联,我用它来总计一个数字。

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

这很好,直到最后:

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

}

我对编程很陌生,有没有人知道可能会搞砸作品?除了最后一个案例之外,整个混乱都是完美的。

1 个答案:

答案 0 :(得分:1)

好吧,我们需要更多信息来帮助您。但是,我假设您正在尝试使用主要活动实施的OnCheckedChangeListener?如果您在引用ID时遇到问题,您可以随时在特定元素上实现单独的lisenter,例如:

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。希望这有助于您的调试。

** * ** 编辑以适应您添加的评论 * < / EM> ** * ** * ** * ***

您是否尝试使用if语句交换switch语句。 e.g。

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

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

}

此外,什么接口onCheckboxClicked(View v),这是一个自定义元素?你是如何在R.layout.activity_main.xml中定义复选框的?如果你有一个复选框界面,你需要像我上面的例子那样实现一个监听器。我无法看到你在任何地方设置听众,也没有在你的活动中实现它。例如:

public class MainActivity extends Activity implements onCheckChangedListener {

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