- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一整行与 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/
我正在使用带数据表的codeigniter,我想按列排序选择。 我怎样才能做到这一点 ? $this->datatables->select('col_1, col_2, col_3'); $this
我是一名优秀的程序员,十分优秀!