我的应用程序中的 SeekBar
未产生正确的结果。 SeekBar
从 0 开始,因此 sbSyst
为每个值添加 70,sbDias
为每个值添加 40。
部分代码:
public void ShowText() {
int inOne = sbSyst.getProgress();
int inTwo = sbDias.getProgress();
if ((inOne >= 0 && inOne <= 20) && (inTwo >= 0 && inTwo <= 20)) {
tvRes.setText("LOW");
tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
}
else if ((inOne >= 21 && inOne <= 50) && (inTwo >= 21 && inTwo <= 40)) {
tvRes.setText("IDEAL");
tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
}
else if ((inOne >= 51 && inOne <= 70) && (inTwo >= 41 && inTwo <= 50)) {
tvRes.setText("PRE-HIGH");
tvRes.setBackgroundColor(Color.parseColor("#D7EF0C"));
}
else if ((inOne >= 71 && inOne <= 120) && (inTwo >= 51 && inTwo <= 60)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#F70729"));
}
}
根据上面的代码,下面应该生成“HIGH”作为文本,而不是显示“IDEAL”
这是整个代码的 Pastebin: http://pastebin.com/C91SpbBg
请帮我解决这个问题。
更新:添加了以下内容并且运行 100%
public void ShowText() {
int inOne = sbSyst.getProgress() + 70;
int inTwo = sbDias.getProgress() + 40;
Log.i("SYSTOLIC SHOWTEXT", String.valueOf(inOne));
Log.i("DIASTOLIC SHOWTEXT", String.valueOf(inTwo));
if ((inOne >= 70 && inOne <= 90) && (inTwo >= 40 && inTwo <= 60)) {
tvRes.setText("LOW");
tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
}
if ((inOne >= 70 && inOne <= 90) && (inTwo >= 61 && inTwo <= 80)) {
tvRes.setText("IDEAL");
tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
}
if ((inOne >= 70 && inOne <= 90) && (inTwo >= 81 && inTwo <= 90)) {
tvRes.setText("PRE-HIGH");
tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
}
if ((inOne >= 70 && inOne <= 90) && (inTwo >= 91 && inTwo <= 100)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#F70729"));
}
if ((inOne >= 91 && inOne <= 120) && (inTwo >= 40 && inTwo <= 80)) {
tvRes.setText("IDEAL");
tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
}
if ((inOne >= 91 && inOne <= 120) && (inTwo >= 81 && inTwo <= 90)) {
tvRes.setText("PRE-HIGH");
tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
}
if ((inOne >= 91 && inOne <= 120) && (inTwo >= 91 && inTwo <= 100)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#F70729"));
}
if ((inOne >= 121 && inOne <= 140) && (inTwo >= 40 && inTwo <= 90)) {
tvRes.setText("PRE-HIGH");
tvRes.setBackgroundColor(Color.parseColor("#C9DF0B"));
}
if ((inOne >= 121 && inOne <= 140) && (inTwo >= 91 && inTwo <= 100)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#F70729"));
}
if ((inOne >= 141 && inOne <= 190) && (inTwo >= 40 && inTwo <= 100)) {
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#F70729"));
}
}
最佳答案
根据上图,得到inOne=82
和inTwo=26
的值。现在查看您的代码,显示 HIGH
:
if ((inOne >= 71 && inOne <= 120) && (inTwo >= 51 && inTwo <= 60))
这不是真的,因为您正在使用 &&
运算符。这意味着两者都应该在高范围内。这就是问题所在。
解决方案:考虑一下当其中一个值位于 High
范围内而另一个值位于 Low
范围内时,您希望如何处理这种情况> 范围等等。然后相应地写下你的逻辑。 (例如:您还可以使用 ||
运算符,这意味着如果其中一个为“HIGH”,则显示为高。)
更新:这是 good source您可以在其中阅读有关解释血压读数的不同 if/else
规则。
更新 2:我有一些空闲时间。以下是基于上述链接的 showText()
方法的实现:
public void ShowText() {
int inOne = sbSyst.getProgress() + 70;
int inTwo = sbDias.getProgress() + 40;
if((inOne >= 140) || (inTwo >= 90)) {
// condition 1: if 140 or more systolic or 90 or more diastolic means HIGH BP
tvRes.setText("HIGH");
tvRes.setBackgroundColor(Color.parseColor("#F70729"));
}
else if((inOne <= 90) || (inTwo <= 60)){
// (condition 1 is not met)
// condition 2: if 90 or less systolic or 60 or less diastolic means LOW BP
tvRes.setText("LOW");
tvRes.setBackgroundColor(Color.parseColor("#7659F5"));
}
else if((inOne >= 120) || (inTwo >= 80)) {
// (condition 1 and 2 are not met ==> NOT HIGH, NOT LOW)
// condition 3: if 120 or more systolic or 80 or more diastolic means PRE-HIGH BP
tvRes.setText("PRE-HIGH");
tvRes.setBackgroundColor(Color.parseColor("#D7EF0C"));
}
else if((inOne >= 90) || (inTwo >= 60)) {
// (condition 1, 2 and 3 are not met ==> NOT HIGH, NOT LOW, NOT PRE-HIGH)
// condition 4: if 90 or more systolic or 60 or more diastolic means IDEAL BP
tvRes.setText("IDEAL");
tvRes.setBackgroundColor(Color.parseColor("#0BA319"));
}
}
关于java - TextView 不会根据 SeekBar 进度自动更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20821311/